【发布时间】:2026-02-12 05:40:01
【问题描述】:
试图从帖子中获取第一张图片,但我的 php 代码没有返回任何内容,有什么帮助吗?
<?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; ?>
不知道出了什么问题,因为我正在使用类似的代码来获取所有图像附件,而不仅仅是一个,而且效果很好。
【问题讨论】: