我在single.php中使用了一个简单的jQuery图像幻灯片,即调用我的帖子的所有图像附件。但我想排除在实际帖子中张贴的图像,但保留所有图像实际附加到帖子。
我使用这段代码来抓取所有的图片附件,但不幸的是,它也抓取了post内容中的所有图片,这是不必要的:
<?php
$attachs = get_posts(array(
'numberposts' => -1,
'post_type' => 'attachment',
'post_parent' => get_the_ID(),
'post_mime_type' => 'image', // get attached images only
'output' => ARRAY_A
));
if (!empty($attachs)) {
foreach ($attachs as $att) {
// get image's source based on size,
// can be 'thumbnail', 'medium', 'large', 'full'
// or registed post thumbnails sizes
$src = wp_get_attachment_image_src($att->ID, 'full');
$src = $src[0];
// show image
echo "<div style='margin: 0 10px 10px 0; float: left'><img src='$src' /></div>";
}
}
?>
有什么建议吗?
发布于 2012-11-19 16:26:57
不幸的是,在Wordpress中,附加的图片和插入到帖子中的图片没有区别。人们在web上使用的一个常见解决方案是向帖子添加一个名为"_inserted_image“(true|false)的meta_data,并在帖子中插入和内联图像时使用钩子来更新此值。在我看来有点太复杂了。
一个简单的解决办法是创建一个专门用于幻灯片放映的类别,并立即将媒体库中用于幻灯片放映的所有图像放入该类别中。然后只需在get posts参数中将'category‘=> 'for_slideshows’添加到您的条件中。
$attachs = get_posts(array(
'numberposts' => -1,
'post_type' => 'attachment',
'post_parent' => get_the_ID(),
'post_mime_type' => 'image', // get attached images only
'output' => ARRAY_A,
'category' => 'for_slideshows'
));
希望能有所帮助。
https://stackoverflow.com/questions/5335207
复制相似问题