Posted in WordPress · January 8th, 2011 by Kazim · Comments (6)
So as I was looking after a simple code to show related post in WordPress. I didn’t find any good little good to do it, where as guys around were providing a huge code to do. I’ve just coded a little piece of code. Hope this will be helpful for you. Let’s start.
It’s so easy to understand. As per WordPress Get Posts function. You can use it for following purposes.
No, this don’t just seem to be useful. This function is really useful for coders. Obviously if a post has same tags means they’re related. For example you’re typing your post for android will contain the tag ‘android’. So when you will create another post on android it will show you the post contains the tags of your current post i.e you’re writing about android and your post tags are android, touch & Wi-Fi. So this will show you all of posts contain tags android, touch & Wifi.
I’m making it easy for you. You will be able to show related post in WordPress by using 3 simple steps below.
<h3> Related Posts</h3> <ul> <?php $tags = wp_get_post_tags($post->ID); global $post; $args = array( 'posts_per_page' => 5, 'tags'=> $tags, 'exclude'=> $post->ID ); $myposts = get_posts( $args ); foreach( $myposts as $post ) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> <?php wp_reset_query(); ?> </ul>
<?php the_tags(
This will benefit your the clicks, More over you can place your 250×250 Google Ads on left side and related post on right side. This will help you increasing your Adsense earning and will also bring up traffic from one to another post.
As per the comment of our good reader, Jitendra Kumar from Shoutingwords saying “The code is good but it’s showing the currently opened post in related post too. Hence need to exclude”. Thanks to Jitendra. After reviewing some more to the code, as I got notified that comment function hasn’t been working fine. The code has now been amended and perfect!
January 10th, 2011 at 8:40 PM
Hi,
First of all thanks for sharing this code. This code is good but it has a problem, it also lists the current page which should not be there since the “current post could not be related to the current post”. To correct this, use the following where you are declaring the array:
$args = array( ‘posts_per_page’ => 5, ‘tags’=> $tags, ‘post__not_in’ => array($post->ID) );
This will correct your code and will not show current post.
January 15th, 2011 at 1:11 PM
Thanks Kazim, the code is perfect now.
May 11th, 2011 at 6:54 AM
Hi,
Just want to ask how to put adsense and related posts side by side. Just like what you do here in your blog.
May 17th, 2011 at 4:14 PM
Dear Raymund,
Regarding the adsense and related post side by side is through float function of css. Likewise you can do float:left to ads and float:right to adsense and giving both divs some width would work! For an example, you can check the source code of this page. You will know it.
Thanks
Kazim
September 22nd, 2011 at 6:22 PM
Do you prefer adding related posts with tags or by category?
September 22nd, 2011 at 6:38 PM
Paul, depends on what you want. Few people prefer to have the related post with tags, cause if you have the tag ‘WordPress’ then other post with the tag wordpress will be displayed. Same goes for the category, If your post is in WordPress category, showing relating post by category would be awesome.
How about an idea of Tag & Category mixup?