$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - Get recent posts with thumbnail|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Get recent posts with thumbnail

matteradmin8PV0评论

I want to ger several recent posts. So I use wp_get_recent_posts. But I get only first image.

<?php $args = array( 'numberposts' => '3' );

$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> ';
        if ( has_post_thumbnail() ) { 
        the_post_thumbnail('thumbnail');
    }
}
?>

I want to ger several recent posts. So I use wp_get_recent_posts. But I get only first image.

<?php $args = array( 'numberposts' => '3' );

$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> ';
        if ( has_post_thumbnail() ) { 
        the_post_thumbnail('thumbnail');
    }
}
?>
Share Improve this question asked Nov 10, 2016 at 23:16 Pravayest PravayestPravayest Pravayest 691 gold badge4 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 7

Actually, the condition is always returning false because you are not passing the post id to the function has_post_thumbnail() and the function always getting the default value which is null.

has_post_thumbnail( $recent["ID"] ).

Same with the function get_the_post_thumbnail().

get_the_post_thumbnail( $recent["ID"] ).


$args = array( 'numberposts' => '3' );

$recent_posts = wp_get_recent_posts($args);

foreach( $recent_posts as $recent ){
   if ( has_post_thumbnail( $recent["ID"]) ) {
      echo  get_the_post_thumbnail($recent["ID"],'thumbnail');
    }
}

But if you use the functions has_post_thumbnail(); and get_the_post_thumbnail() inside the WordPress The_Loop then you don't need to pass the post id.


$args = array( 'posts_per_page' => '3' );
$recent_posts = new WP_Query($args);
while( $recent_posts->have_posts() ) { 

   $recent_posts->the_post() ; 

   if ( has_post_thumbnail() ) {
      echo  get_the_post_thumbnail();
    }
}

wp_reset_postdata();

In order to use the_post_thumbnail, you need to initialize a loop. So more like this:

<?php 
$args = array( 'posts_per_page' => '3' );
$recent_posts = new WP_Query($args);
while( $recent_posts->have_posts() ) :  
    $recent_posts->the_post() ?>
    <li>
        <a href="<?php echo get_permalink() ?>"><?php the_title() ?></a> 
        <?php if ( has_post_thumbnail() ) : ?>
            <?php the_post_thumbnail('thumbnail') ?>
        <?php endif ?>    
    </li>
<?php endwhile; ?>
<?php wp_reset_postdata(); # reset post data so that other queries/loops work ?> 

(I put the thumbnail inside the <li> tags because anything besides <li> inside a <ol> or <ul> is invalid html.)

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far