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?
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?
- Login to your WordPress admin panel say www.xyz/wp-admin/
- Go to Appearance section and click theme editor
- 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
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
<?php the_content(); ?>
<?php relatedPostsbyCategory();?></div>
Easily add the below code to your functions.php file which is located at yourwebsitename.com/wp-admin/theme-editor.php
function
joints_related_posts() {
global
$post
;
$tags
= wp_get_post_tags(
$post
->ID );
if
(
$tags
) {
foreach
(
$tags
as
$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_posts
as
$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>'
;
}
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
<?php the_content(); ?>
<?php joints_related_posts();?></div>
[/fusion_text][/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]