我正在寻找一种解决方案,使我能够获取附加到循环中帖子的第一张图像的ID。
我目前正在使用这个;
$size = \'post-thumbnail\'; // Define image_size to be used
$args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'post_parent\' => $post->ID
);
$images = get_posts($args);
$attachment_id = $images[0]->ID;
$i = wp_get_attachment_image_src($attachment_id, $size);
if (has_post_thumbnail()) { // Check if post thumbnail is present first
the_post_thumbnail($size);
} else { // If no post thumbnail is present, echo first image in post
echo \'<img src="\'.$i[0].\'" alt=""/>\';
}
如果它在单个帖子上运行,则效果很好,但在帖子循环中无法正常运行(它只在1个帖子上获取ID,而不是所有帖子)。我在google上搜索了一下,发现的每个函数似乎都有相同的问题,它只获得了一个帖子的ID。
有人能帮我吗?
这需要是一个函数,而不是插件。