Display related posts in wordpress without plugin
How to display related post in wordpress without plugin? Displaying related posts in wordpress blog is a very great way to help the visitors staying longer on your blog. It is also can increase your blog page views. You can use a related post plugin, but you also can use tags and custom code to show related posts in your wordpress. Let’s see how to do that.
Copy this code to your wordpress theme code anywhere to show related post:
<?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”>
<b>Related Posts</b>
<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);
?>
That code work to display related post in your wordpress blog, i hope this is useful and thanks.