WordPress related posts on theme
Displaying related posts can be very useful to visitors which could help them to find relevant contents they are interested in. Here is the php code how to fetch related contents for the displayed post and show them in the list.
Here is how you can show related posts on the posts detail page on your wordpress site. Here we will write some wordpress query on theme files to show related posts. Relates posts are the posts which share common tags. Open up the single.php from your theme folder. single.php is file used to show post detail page on wordpress. Locate the position where you want to show related posts. In my case it is the position just above the comments. Put the php code to include related.php file. related.php is the file where you actually put code which renders related posts of a given post.
Code to display related posts (related.php)
ID);
if ($tags) {
echo 'Related Posts
';
$tagids="";
foreach($tags as $tag)
$tagids= $tagids.$tag->term_id.',';
$args=array('tag__in' => array($tagids),'post__not_in' => array($post->ID),'showposts'=>9,'caller_get_posts'=>1);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
ID);if ($tags) { echo 'Related Posts
'; $tagids=""; foreach($tags as $tag) $tagids= $tagids.$tag->term_id.','; $args=array( 'tag__in' => array($tagids), 'post__not_in' => array($post->ID), 'showposts'=>9, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?>