Display Related Posts in WordPress

Add Tool

How to Display Related Posts in WordPress


How to Display Related Posts in WordPress

WordPress related posts is the best way to keep visitors in website. When your visitors will read a post they will see another related posts of the category. It may be helpful for them. I have seen many popular and High PR (Page rank) websites, they are using wordpress related posts at the end of single post. Here I have shared some php codes which will display related posts without using any related post plugin.

How To Display WordPress Related Posts By Category

To display related posts of every single page simply add the below php codes in your theme’s functions.php file

<?php
// "More from This Category" list by Barış Ünver @ devtoolsplus+
function devtoolsplus_more_from_cat( $title = "More From This Category:" ) {
    global $post;
    // We should get the first category of the post
    $categories = get_the_category( $post->ID );
    $first_cat = $categories[0]->cat_ID;
    // Let's start the $output by displaying the title and opening the <ul>
    $output = '<div id="more-from-cat"><h3>' . $title . '</h3>';
    // The arguments of the post list!
    $args = array(
        // It should be in the first category of our post:
        'category__in' => array( $first_cat ),
        // Our post should NOT be in the list:
        'post__not_in' => array( $post->ID ),
        // ...And it should fetch 5 posts - you can change this number if you like:
        'posts_per_page' => 5
    );
    // The get_posts() function
    $posts = get_posts( $args );
    if( $posts ) {
        $output .= '<ul>';
        // Let's start the loop!
        foreach( $posts as $post ) {
            setup_postdata( $post );
            $post_title = get_the_title();
            $permalink = get_permalink();
            $output .= '<li><a href="' . $permalink . '" title="' . esc_attr( $post_title ) . '">' . $post_title . '</a></li>';
        }
        $output .= '</ul>';
    } else {
        // If there are no posts, we should return something, too!
        $output .= '<p>Sorry, this category has just one post and you just read it!</p>';
    }
    // Let's close the <div> and return the $output:
    $output .= '</div>';
    return $output;
}
?>

After adding this codes in your theme’s functions.php file open your theme’s single.php file and add the below codes where you'd like to display the related posts

<?php echo devtoolsplus_more_from_cat( 'More From This  Category:' ); ?>

This codes will display 5 WordPress related posts in every single page. If you want more than 5 post find 'posts_per_page' => 5 and replace 5 to your own number.

See Also: Display WordPress related posts By Current Author

Now need to add some css in style.css file. So open your theme’s style.css file and add the below css codes

  #more-from-cat li {
  list-style:  decimal;
  font-size:  11pt;
  }

That enough to display WordPress related posts. Now you can add style by using the above css codes. If you weak in css you can learn css in our css section.

Thanks to WP Tuts for this wordpress related posts tutorial


Tags: related posts WordPress, Display Related Posts, Display Related Posts in WordPress, wordpress related posts widget
Payza

About Masum Billah

How to Display Related Posts in WordPress Hi I am Md. Masum Billah Contributor of this website. I always like to share various things with other. In this website I am sharing web design related tips and tricks, necessary web development tools, source codes for beginner and advance web developer.


SIGN UP TO GET UPDATE TO YOUR INBOX




Discuss about: Display Related Posts in WordPress


adf.ly