WordPress – Retrieve featured images for posts listed by category

I am new to WordPress and have successfully retrieved three posts for every category and listed them. I also want to retrieve the featured image for every post. I have tried out the following code in the index.php file. The code retrieved the three posts as I have mentioned, but the code to retrieve the images just appeared as plain text on the website, so I assume I have some syntax error. Can anyone help me out? Any help is appreciated.


//get all terms (e.g. categories or post tags), then display all posts in each term
$taxonomy = 'category';//  e.g. post_tag, category
$param_type = 'category__in'; //  e.g. tag__in, category__in
  'orderby' => 'name',
  'order' => 'ASC'
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
  foreach( $terms as $term ) {
      "$param_type" => array($term->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 3,
      'caller_get_posts'=> 1,
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts in '.$taxonomy .' '.$term->name;
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>

if (has_post_thumbnail($post->ID)) {
        $retina  = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'homepage-thumb-retina' );
        echo '<img src="' . $retina[0] . '" alt="' . the_title() . '" width="24" height="24" />' ;

wp_reset_query();  // Restore global post data stomped by the_post().

Read more here: WordPress – Retrieve featured images for posts listed by category

Leave a Reply

Your email address will not be published. Required fields are marked *