试图从帖子中获取第一张图片,但我的php代码不会返回任何内容,有帮助吗?Wordpress从帖子中获取第一张图片
<?php while ($browndog_blog->have_posts()) : $browndog_blog->the_post();
$args = array(
'numberposts' => 1,
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_status' => null,
'post_type' => 'attachment'
);
$attachments = get_children($args);
//print_r($attachments);
if ($attachments) {
foreach($attachments as $attachment) {
$image_attributes = wp_get_attachment_image_src($attachment->ID, 'thumbnail') ? wp_get_attachment_image_src($attachment->ID, 'thumbnail') : wp_get_attachment_image_src($attachment->ID, 'full');
echo '<a href="'.get_permalink($post->ID).'"><img src="'.wp_get_attachment_thumb_url($attachment->ID).'"></a>';
echo '<p>'.get_the_excerpt($post->ID).'</p>';
echo '<p><a href="'.get_permalink($post->ID).'">Read More</a></p>';
}
}
endwhile; ?>
不知道发生了什么事情错了,因为我使用了一个类似的代码来获取所有图像附件,而不是只有一个,那工作正常。
[如何获得第一张图片与WP帖子相关联]的可能重复?(http://stackoverflow.com/questions/2332979/how-to-get-the-first-image-assoc-with-a -wp-POST) – hakre