LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » WordPress / WordPress Snippets » Add Full Featured Related Posts In WordPress Without Plugin

Add Full Featured Related Posts In WordPress Without Plugin

Add-Full-Featured-Related-Posts-In-WordPress-Without-Plugin
Related posts are soo important in view of SEO that you cant imagine. Its reduces your bounce rates and increase page views. Its also increase clicks and time on your site/blog. So if you are thinking to avoid this then you are doing a major mistake in SEO way so you must have these on your blogs.

Now there are many plugins to add related post but if you know basics then why to go with plugin when without plugin way is easy and available. Here we have two related posts snippet that is very ways to add and use. Its originally works like charm. They first pick your post tags or categories then after picking that find the other posts in that category then pick other posts of that tags and categories and show them under your main post.

Its is well optimized codes so that you can also use it in your WP_Loops also and they will not break your pages. It contain also a lot of features like you can control that how many posts you want to show as related post, you can also control what to show in related posts as from thumbnails, titles, description, author, date, comments count.

Many other related posts only pick the latest post from your main posts categories/tags and show them so at reload of the same page your random posts stay same but this code will display random posts from your main post categories/tags. You can also add your CSS on it so now without any more preface, just follow the easy tutorial and add the related posts now in your WordPress blog easily.

Features:

1.) Pure PHP code.
2.) Have to edit single.php file.
3.) Add Related Posts on your all post page.
4.) Pick related post from main posts tags/categories.
5.) Easy to use and fully customizable.
6.) Can be placed in Wp_Loop also.
7.) SEO supported.
8.) Decreases bounce rates and increase Pageviews.
9.) Will display thumbnails, titles, description, author, date, comments count.
10.) You can choose easily what to show.

How To Add Full Featured Related Posts In WordPress Without Plugin?

Step 1:) First of all open your WordPress blogvia Admin.
Step 2:) Now go to Appearance > Editor.
Step 3:) Here open single.php to edit.
Step 4:) Now Copy any one code from the below codes and Paste it after <?php the_content(); ?> in your Single Post(single.php) file.

1.) Related Posts by Tags:

WordPress has this amazing taxonomy known as “Post Tags” which you can use to pick your main post tags and then show the other posts under those tags. So if you want to do this then use the below code.

<?php $orig_post = $post;
global $post;
$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),
'posts_per_page'=>5, // Number of related posts that will be shown.
'ignore_sticky_posts' => 1,
'orderby' => 'rand'
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo '<div id="related_posts"><h2>Dont Forget To Read These Also...</h2><ul>';
while( $my_query->have_posts() ) {
$my_query->the_post();?>
<li>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>" alt="<?php the_title(); ?>">
<?php if ( has_post_thumbnail() ): ?>
<?php the_post_thumbnail('medium'); ?>
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/No-Thumbnail-Available.png" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"/>
<?php endif; ?>
</a>
<h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
<span class="post_desc"><?php the_excerpt(); ?></span>
<span class="post_info">
<span class="post_author"><?php the_author_posts_link(); ?></span> | 
<span class="post_date"><?php the_time('F jS, Y') ?></span> | 
<span class="post_comment"><a href="<?php comments_link(); ?>"><?php comments_number( '0', '1', '%' ); ?></a></span>
</span>
<div class="clear"></div>
</li>
<?php }
echo '</ul></div>';
}
}
$post = $orig_post;
wp_reset_query(); ?>

2.) Related Posts by Category:

WordPress also has this amazing taxonomy known as “Post Categories” which you can use to pick your main post categories and then show the other posts under those tags. So if you want to do this then use the below code.

<?php $orig_post = $post;
global $post;
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=> 2, // Number of related posts that will be shown.
'ignore_sticky_posts' => 1,
'orderby' => 'rand'
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo '<div id="related_posts"><h2>Dont Forget To Read These Also...</h2><ul>';
while( $my_query->have_posts() ) {
$my_query->the_post();?>
<li>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>" alt="<?php the_title(); ?>">
<?php if ( has_post_thumbnail() ): ?>
<?php the_post_thumbnail('medium'); ?>
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/No-Thumbnail-Available.png" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"/>
<?php endif; ?>
</a>
<h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
<span class="post_desc"><?php the_excerpt(); ?></span>
<span class="post_info">
<span class="post_author"><?php the_author_posts_link(); ?></span> | 
<span class="post_date"><?php the_time('F jS, Y') ?></span> | 
<span class="post_comment"><a href="<?php comments_link(); ?>"><?php comments_number( '0', '1', '%' ); ?></a></span>
</span>
<div class="clear"></div>
</li>
<?php }
echo '</ul></div>';
}
}
$post = $orig_post;
wp_reset_query(); ?>

Step 5:) Now save and done.

Customization:

You can edit it with only those features that you want. You also have to add some CSS on it to make it more cool. Rest all is done.

Theme Files Editing Warning:

Keep in mind that every code that you will add in your themes files will be removed when you will change the theme. So after changing your theme, you have to follow this article again and you will get everything back same as it is now.

Last Words:

This is what we have and shared in easy steps for newbies so that they can easily know how it works. Stay with us because we are going to share a whole guide step by step about WordPress and make it easy for you. If you liked it then share it and be with us to get next tutorial. If you have any problem then feel free to ask us. We will help you with what we can or have.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *