In this tutorial, I am going to share how to Display Related Posts without using any plugin in WordPress. If you want to display the related posts after the end of every post’s content, then copy the below code and paste it at the in your single-post & single page file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //for use in the loop, list 4 post titles related to first tag on current post $tags = wp_get_post_tags($post->ID); if ($tags) { echo '<h3>Other people are reading…</h3>'; $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'posts_per_page'=>4, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <ul> <li><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"> <?php the_title(); ?></a> </li> </ul> <?php endwhile; } wp_reset_query(); } |
I hope this tutorial helps you, if you have any questions regarding WordPress, then feel free to put your comments in below comment section. Do you like & share this article with your friends, and don’t forget to follow us on Facebook and Twitter to learn cool WordPress tutorials.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.