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

()

[fusion_builder_container hundred_percent=”no” equal_height_columns=”no” menu_anchor=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” background_color=”” background_image=”” background_position=”center center” background_repeat=”no-repeat” fade=”no” background_parallax=”none” parallax_speed=”0.3″ video_mp4=”” video_webm=”” video_ogv=”” video_url=”” video_aspect_ratio=”16:9″ video_loop=”yes” video_mute=”yes” overlay_color=”” video_preview_image=”” border_size=”” border_color=”” border_style=”solid” padding_top=”” padding_bottom=”” padding_left=”” padding_right=””][fusion_builder_row][fusion_builder_column type=”1_1″ layout=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” border_position=”all” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding_top=”” padding_right=”” padding_bottom=”” padding_left=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” center_content=”no” last=”no” min_height=”” hover_type=”none” link=””][fusion_text]

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 [email protected]/skype-bpshbp today

[/fusion_text][/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]

Must READ  How to create child theme in Wordpress Step by Step without Wordpress plugin

How useful was this post?

Click on a star to rate it!

Admin

For paid sponsorship & partnership, email our board of admin at [email protected] , [email protected] . Regards. :)

Leave a Reply

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