In this post, I am going to explain how to Display Related Posts on post single page in WordPress. In WordPress you can display related posts using plugin because lots of plugin available of WordPress. But in this post we can show you how to display related posts without any plugin.
There are two methods of display related post on single post page and you can choose whichever method you prefer. One way is doing it with a plugin and then there is another method that is doing it without any plugin.
Display Related Posts in WordPress without any Plugin
Please copy the below code and paste the following code in single.php where you want to display related post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php //This code use in the loop, list 5 post titles related to first tag on current post $tags = wp_get_post_tags($post->ID); if ($tags) { echo '<h1>Related Posts</h1>'; $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'posts_per_page'=>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(); ?> <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> <?php endwhile; } wp_reset_query(); } ?> |
I have used WP_query function for display related posts on single page of post.
We hope that this article is helpful for WordPress beginners. You may also want to see our other posts of WordPress then click https://webprepration.com/category/wordpress/ and also you can follow on social sites etc.
Thanks!!!
I’m a full-stack developer. My hobby and profession to write blog and programming tips that helps to others. I am a great admirer of PHP, Laravel, Codeigniter, AngularJS, Vue.js, Javascript, JQuery, WordPress, Plugin Development, Theme Development and Bootstrap from the early stage.