使用缩略图wordpress显示最新帖子


display latest post with thumbnail wordpress

我想用短代码显示Wordpress博客的3篇最新帖子。我写了这个php代码。它可以工作,但每个帖子显示3次。你知道怎么修吗?

$counter = 3;
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
$recent_posts = wp_get_recent_posts(3);
while ($recentPosts->have_posts()) : $recentPosts->the_post();
foreach( $recent_posts as $recent ){
echo '<li class="box'.$counter--.'">';
the_post_thumbnail();
echo '<a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a>';
echo '</li>';
}
endwhile;

请使用wordpress默认函数wp_get_recent_posts使用此代码

<h2>Latest Posts</h2>
<ul>
<?php
    $args = array(
        'numberposts' => 3,
    );
    $recent_posts = wp_get_recent_posts($args, $output = ARRAY_A);
    foreach( $recent_posts as $recent ){
        echo get_the_post_thumbnail( $recent["ID"], 'thumbnail' );
        echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a> </li> ';
    }
?>
</ul>

还可以在需要显示的位置调整代码。

请尝试以下代码。

<?php
 $postslist = get_posts('numberposts=3&order=DESC&orderby=date');
 foreach ($postslist as $post) :
    setup_postdata($post);
 ?>
 <div class="entry">
 <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
 <?php the_time(get_option('date_format')) ?>
 <?php the_excerpt(); ?>
 </div>
 <?php endforeach; ?>

请尝试此代码。

<?php
    $args = array( 'numberposts' => '5' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
        echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a> </li> ';
    }
?>