How To Display Related Posts From a Category single.php – WordPress Tricks

Please log in or register to like posts.
News

Hi wordpress beginners, today i’m going to show you how to get related posts from single.php file. first we are going to know what is related posts system in wordpress, and how it works/why it’s useful. WordPress is a popular content management system we know this. we can use the wordpress functions or own use own functions from php. wordpress required each theme files must need to have functions.php file to run a theme in wordpress website.

 

What is Related Post Function?

wordpress theme have a file that called single.php, in this file are showing each posts meta tag, descriptions, output design and much more that you are adding. but if you have a category and posted more than 5, may you want to display related posts from this category in post reading system providing single.php file. related posts function will help you to call your category related post from single.php file. the related posts function will help you to display category related posts that reading similar. hope you are understand what is the related posts function..! šŸ˜‰

 

How useful Related Posts Function?

above i was told you about the related post function and how does it works. So related post call the category related posts from a post, you can call it to wordpress similar posts system also. If you don’t know about Search Engine Optimization please read the SEO From our posted articles. hope you can understand the related posts functions from SEO Optimization Guide. WordPress Related Posts Will Help you to make your wordpress theme seo friendly.

 

Related Post

Now We are going to setting up the wordpress related post in our single.php file. at first copy past the below code and place it to single.php file anywhere you want to display.

<?php  //for use in the loop, list 5 post titles related to first tag on current post
  $backup = $post;  // backup the current object
  $tags = wp_get_post_tags($post->ID);
  $tagIDs = array();
  if ($tags) {
    $tagcount = count($tags);
    for ($i = 0; $i < $tagcount; $i++) {
      $tagIDs[$i] = $tags[$i]->term_id;
    }
    $args=array(
      'tag__in' => $tagIDs,
      'post__not_in' => array($post->ID),
      'showposts'=>5,
      'caller_get_posts'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
      <?php endwhile;
    } else { ?>
      <h2>No related posts found!</h2>
    <?php }
  }
  $post = $backup;  // copy it back
  wp_reset_query(); // to use the original query again
?>

Above Code display 5 posts in a single post. if you want to make your own posts limit, simply replace showposts number.

If you want to make a dynamic related posts option, make a file under includes folder on your theme file, i’m called it to cat-posts.php and copy past the above code. then call the file from your single.php file by placing the below template part.

<?php get_template_part( 'includes/cat-posts' ); ?>

Hope The Tutorial will help you a lot. For More WordPress Tricks Read Some Below Posts.

10+ Most Wanted WPĀ  Tips, Tricks, and Hacks ā€“ HowTrick
How to Fix WP 404 Post Returning Error Issue
How to fix Establish Database Connection in WP
What is Internal server error and how to fix ā€“ WP
How To Display Random Posts in WP ā€“ Coding
How to Change the WP Login Logo ā€“ Custom Logo
WordPress Theme Development Ā« WP Codex
8 Extremely Useful WP Functions For Developers

[ads]

Nobody liked ?

2 comments on “How To Display Related Posts From a Category single.php – WordPress Tricks

  1. I am reading your website while on a boat. On a scale from 1 to 10, You are an 11. Your blog has a certian aura about it. You have a great sense of humor.

Leave a Reply

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