試圖從帖子中獲取第一張圖片,但我的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