Display Related Posts with Thumbnails from Current Post Category

Using the below PHP code, we can generate related post links from the current post category and add them just after the post content.

This can be achieved by showing related posts link from the category of current post. Showing related posts will definitely help the user in finding more relevant information from your website.

Just add the below code after <?php the_content(); ?> line in single.php or in the required page template file. If you refresh the page, you would see the related post links showing up just after the post content.

If you working under Child theme, you could get your single.php from parent theme and add it to child themes directory & make changes on top of it. This will display related posts with Thumbnails without plugins.

<?php
    $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 6, 'post__not_in' => array($post->ID) ) );
    if( $related ) foreach( $related as $post ) {
    setup_postdata($post); ?>
     <ul style="list-style: none;">
        <li style="float:left;padding:5px;width:120px;">
        <a href="<?php the_permalink() ?>" style="color:blue;" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail();?><?php the_title(); ?></a>

        </li>
        </ul>
    <?php }
    wp_reset_postdata(); ?>

Though there are links to categories or tag available in the sidebar/footer, their accessibility is less when compared to the links posted just after the end of the content. Related posts will increase the user engagement of the website and it will also thrive to few additional page views for the website.

Also Read: Display Recent Posts with Thumbnails WordPress

Code Explanation:

  • get_posts( array( ‘category__in’ => wp_get_post_categories($post->ID), ‘numberposts’ => 5, ‘post__not_in’ => array($post->ID) ) )
    • ‘category__in’ will be replaced with the current post category id by the function wp_get_post_categories($post->ID).
    • Number of posts to be retrieved can be defined in the parameter ‘numberposts’
    • ‘post__not_in’ is added to remove the current page link in the returning related post links
  • while looping through the returned related post link, setup_postdata($post) sets the global variable $post to the current iterated post. So, if we use the functions <?php the_permalink();?> and <?php the_title();?>, it will retrieve the title and permalink from $post variable which is currently pointing to iterated post.
  • wp_reset_postdata() resets the global variable $post back to the previous status ( $post will usually hold the pointer to the current page). This line is very important, missing this will collapse the working of other functionalities’, so please ensure that it is taken care.
  • By directly placing the values for ‘category__in’  with the category id, we can gather links pertaining to a category id. So, it can be customized to get the links from current post category or a specific category.
  • the_post_thumbnail() will retrieve the Featured Image set for this post.

Note: We can choose to show related posts at end of the content or We can add this code snippet to sidebar or in any location in the chosen template files.

Engage the Visitor a Little Longer in Your Site

Show Related Post in PHP WordPress
Display Related Posts in WordPress

In addition to Focusing on Quality content in your Website, You should also focus on improving the engagement time of each visitor in your website. To Achieve this, You can add reference links to other pages of similar interest in your website.

User might’ve landed in a page through Google search or any other forums to find the answer for his search keyword. Once the user gets hold of the information from Your site, show them a way to read related articles.

 Reference:

Codex – http://codex.wordpress.org/Function_Reference/setup_postdata

We can easily show related posts from the current post category by updating the PHP template files.

Mote Tips:  Create Shortcode for Google Ad Units in WordPress


2 thoughts on “Display Related Posts with Thumbnails in WordPress without Plugins”

  1. Hi,
    Sorry for the delay in answering this.
    To get the thumbnail displayed, we need to use the method the_post_thumbnail(). I’ve updated the code to show thumbnails also along with post title. please try it and share your updates.

Leave a Reply