Support » Fixing WordPress » How do I include thumbnail in Latest Post shortcode?

  • I have successfully created a shortcode in my theme for displaying the latest post. Unfortunately, I can’t get the post’s Featured Image (AKA thumbnail) to display. Any thoughts or advice would be greatly appreciated.

    // Short Code for Latest Post
    add_theme_support( 'post-thumbnails' );
    function my_recent_posts_shortcode($atts){
    
     $q = new WP_Query(
       array( 'orderby' => 'date', 'posts_per_page' => '1')
     );
    
    $list = '<div class="recent-posts">';
    
    	while($q->have_posts()) : $q->the_post();
    		$list .= '<a href="' . get_permalink() . '">' . get_the_post_thumbnail('thumbnail') . ' ' . get_the_title() . '</a>';
    
    	endwhile;
    
    	wp_reset_query();
    
    return $list . '</div>';
    }
    
    add_shortcode('recent-posts', 'my_recent_posts_shortcode');
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How do I include thumbnail in Latest Post shortcode?’ is closed to new replies.