在Wordpress中显示文章缩略图只显示第一个缩略图


Displaying posts thumbnails in Wordpress show only first thumbnail

我想在侧边栏上显示最新的帖子标题和缩略图。到目前为止,我得到的帖子标题和只有一个缩略图重复。你可以在这里看到结果。(仅显示第一个/最旧的后期图像)

这是我的代码:

$rps = wp_get_recent_posts($params);
foreach($rps as $rp) :
    $args = array(
            'post_type' => 'attachment',
            'numberposts' => 1,
            'post_status' => null,
            'post_parent' => $post->ID
             );
    $attachment = current(get_posts( $args ));
?>
<a href="<?php echo get_permalink($rp['ID']);?>"><?php echo $rp['post_title'];?><?php echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );?></a>
<?php endforeach; ?>

感谢您提供的任何提示/帮助。

'post_parent' => $post->ID替换为'post_parent' => $rp['ID']。就是这样。您正在做的是,将当前帖子的ID以$args的形式传递给所有帖子。

运行2个查询

一个输出第一个帖子。第二个输出除第一个之外的所有其他内容

<?php $args = array( 'post_type' => 'attachment', 'posts_per_page' => 1, 'post_parent' => $post->ID);  
$first = new WP_Query( $args ); 
while ( $first->have_posts() ) : $first->the_post(); ?>
<a href="<?php echo get_permalink();?>"><?php the_title();?><?php echo wp_get_attachment_image( $first->ID, 'thumbnail' );?></a>
<?php endwhile; wp_reset_postdata(); ?>
<?php $args2 = array( 'post_type' => 'attachment', 'posts_per_page' => 1, 'post_parent' => $post->ID, 'offset' => 1);  
$rest = new WP_Query( $args2 );
while ( $rest->have_posts() ) : $rest->the_post(); ?>
<a href="<?php echo get_permalink();?>"><?php the_title();?><?php echo wp_get_attachment_image( $rest->ID, 'thumbnail' );?></a>
 <?php endwhile; wp_reset_postdata(); ?>