Trick to Display WordPress Related Posts by category,tag without Plugin

Display WordPress Related Posts by category without Plugin Easily

Why use WordPress without Plugin?

Display WordPress Related Posts by category,tag without Plugin

Discovering a excellent WordPress Related Posts by category plugin will  be BIG TASK. Many WordPress Related Posts by category plugin include predefined predefined styles or a rigid structure hard to integrate into your WordPress theme. Even worse, most of these WordPress plugins are tremendously database intensive and some webhosting company won’t even allow to add most “related submit” plugins on their servers (for security ,etc reasons).

How to Display WordPress Related Posts by category without Plugin?

  1. Login to your WordPress admin panel say www.xyz/wp-admin/
  2. Go to Appearance section and click theme editor
  3. Open your theme’s functions.php and paste the below  code inside this  <?php                             ?>:

Easily add the below code to your functions.php file which is located at yourwebsitename.com/wp-admin/theme-editor.php

function relatedPostsbyCategory(){
$original_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.
‘caller_get_posts’=>1
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo ‘<div id=”related_posts”><h3>Related Posts</h3><ul>’;
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<li><div class=“relatedthumb”><a href=<? the_permalink()?> rel=“bookmark” title=<?php the_title(); ?>><?php the_post_thumbnail(); ?></a></div>
<div class=“relatedcontent”>
<h3><a href=<? the_permalink(); ?> rel=“bookmark” title=<?php the_title(); ?>><?php the_title(); ?></a></h3>
<?php the_time(‘M j, Y’); ?>
</div>
</li>
<?php
}
echo ‘</ul></div>’;
}
}
$post = $original_post;
wp_reset_query();
}
Note-
If you want to increase number of Related Posts per page ,then in above code just change number ‘posts_per_page’
Now to Display WordPress Related Posts at the end of the article you will need to put below code any where in single.php
<?php relatedPostsbyCategory();?>

Below is the example where in www.Share-ask.com web site we have placed  code   in single.php file to display Related Post  <?php relatedPostsbyCategory();?> at the end of the article

<div class=”thecontent”>
<?php the_content(); ?>
<?php relatedPostsbyCategory();?></div>
How to Display WordPress Related Posts by tag without Plugin?

Easily add the below code to your functions.php file which is located at yourwebsitename.com/wp-admin/theme-editor.php

functionjoints_related_posts() {
    global$post;
    $tags= wp_get_post_tags( $post->ID );
    if($tags) {
        foreach( $tagsas$tag) {
            $tag_arr.= $tag->slug . ',';
        }
        $args= array(
            'tag'=> $tag_arr,
            'numberposts'=> 3, /* You can change this to show more */
            'post__not_in'=> array($post->ID)
        );
        $related_posts= get_posts( $args);
        if($related_posts) {
        echo'<h4>Related Posts</h4>';
        echo'<ul id="joints-related-posts">';
            foreach( $related_postsas$post) : setup_postdata( $post); ?>
                <li class="related_post">
                    <a class="entry-unrelated"href="<?php the_permalink() ?>"title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                    <?php get_template_part( 'partials/content', 'byline'); ?>
                </li>
            <?php endforeach; }
            }
    wp_reset_postdata();
    echo'</ul>';
}
Note-
If you want to increase number of Related posts per page,then in above code just change number in ‘numberposts’ 
This code will not display  related posts if no related posts are found.
Now to Display WordPress Related Posts at the end of the article you will need to put below code any where in single.php –
<?php joints_related_posts();?>

Below is the example where in www.Share-ask.com web site we have placed  code   in single.php file to display Related Post  <?php joints_related_posts();?> at the end of the article

<div class=”thecontent”>
<?php the_content(); ?>
<?php joints_related_posts();?></div>
If you are gettng any error  PM me to chat-besumitbe@gmail.com/skype-bpshbp today

Urgent APPEAL-Help Save our WebSite

Donate any amount