Display Related Post In Wordpress Without Plugin
One of the reason that makes your Wordpress blog takes long time to load is too many plugin installed in your dashboard, so it will better to have minimum plugin possible to reduce loading times, and one of which is by using PHP code to display related post instead of using related post plugin.
And the following are 2 methods to display related post based on category and tag, and I will firstly warn you to back up your file before start doing anything to your Single.php file, or else you’ll be sorry if anything goes wrong, ok let’s get started.
1. Displaying related post based on Category (Categories):
Login to your Wordpress dashboard , go to your Appearance-> Editor and open your Single.php file, and add this code beneath your post content, like after this tag for example: <?php the_content() ?>:
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 26 27 28 | <?php
$this_post = $post;
$category = get_the_category(); $category = $category[0]; $category = $category->cat_ID;
$posts = get_posts('numberposts=6&offset=0&orderby=post_date&order=DESC&category='.$category);
$count = 0;
foreach ( $posts as $post ) {
if ( $post->ID == $this_post->ID || $count == 5) {
unset($posts[$count]);
}else{
$count ++;
}
}
?>
<?php if ( $posts ) : ?>
<div class="related_articles">
<h2>Related Posts</h2>
<ul>
<?php foreach ( $posts as $post ) : ?>
<li><a href="<?php the_permalink() ?>" title="<?php echo trim(str_replace("n"," ",preg_replace('#<[^>]*?>#si','',get_the_excerpt()))) ?>"><?php if ( get_the_title() ){ the_title(); }else{ echo "Untitled"; } ?></a> (<?php the_time('F jS, Y') ?>)</li>
<?php endforeach // $posts as $post ?>
</ul>
</div>
<?php endif // $posts ?>
<?php
$post = $this_post;
unset($this_post);
?> |
2. Displaying related post based on Tag (Tags):
Do exactly the same as the above steps and add this PHP code inside your Single.php file:
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
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'showposts'=>5, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h3>Related Posts</h3><ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
}
}
?> |
Then click this -> Update File or Save.
Now your related posts will appear in each of your post. Hope this post useful to you, and see you in the next post.
Related posts:









6 Responses
8.29.2009
It’s very useful, especially Displaying related post based on Category . I tried to find the code 4 long. Many Tks!
12.2.2009
Wat is the code when I want to show the related posts with image and the content?
Thanks,
Robert
1.14.2010
Thankyou mate, I was puzzled over how to do this for 2days, this works a charm, cheers.
1.21.2010
Thanks for this code. I tried it and it works perfectly. My only question, and I hope you can help, is I’m trying to get the related post by tag, within a certain category.
What would I need to add to the above code to achieve that? Thanks in advance.
2.3.2010
I am sorry my friend I don’t know how to achieve that