{"id":182,"date":"2020-08-08T17:20:35","date_gmt":"2020-08-08T17:20:35","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=182"},"modified":"2020-08-20T09:15:32","modified_gmt":"2020-08-20T09:15:32","slug":"how-to-display-related-posts-from-same-category-in-sidebar-without-plugin-in-wordpress","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/how-to-display-related-posts-from-same-category-in-sidebar-without-plugin-in-wordpress\/","title":{"rendered":"How to Display Related Posts From Same Category in the Sidebar in WordPress"},"content":{"rendered":"\n<p>Here is a simple working way to display related posts from same category in sidebar without plugin in WordPress. For each post you can display related posts (of same category) in the sidebar.<\/p>\n\n\n\n<p><strong>1. Add following  code in your wordPress theme&#8217;s <\/strong><em><strong>functions.php<\/strong><\/em><strong> file:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>function show_related_post() {\n\t$args = array(\n                'category__in' => wp_get_post_categories( get_queried_object_id() ),\n                'posts_per_page' => 5,\n                'orderby'       => 'rand',\n                'post__not_in' => array( get_queried_object_id() )\n                );\n    $the_query = new WP_Query( $args );\n    if ( $the_query->have_posts() ) : ?> \n\t&lt;section id=\"recent-posts\" class=\"widget widget_recent_entries\">\t\t \n        &lt;h2 class=\"widget-title\">Related Articles&lt;\/h2>\n        &lt;ul class=\"\">\n        &lt;!-- the loop -->\n       &lt;?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>\n            &lt;li>\n                &lt;a href=\"&lt;?php the_permalink(); ?>\" title=\"&lt;?php the_title_attribute(); ?>\">\n                    &lt;?php the_title(); ?>\n                 &lt;\/a>\n            &lt;\/li>\n        &lt;?php endwhile; ?>\n        &lt;!-- end of the loop -->\n        &lt;\/ul>\n\t&lt;\/section>\n        &lt;?php wp_reset_postdata(); ?>\n     &lt;?php endif; \n} ?><\/code><\/pre>\n\n\n\n<p><strong>2.  Add the following code in the <\/strong><em><strong>wp-includes\/widgets\/class-wp-widget-recent-posts.php<\/strong><\/em><strong> file at the beginning of <\/strong><em><strong>widget <\/strong><\/em><strong>function:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>public function widget( $args, $instance ) \n\/\/ Code to display related posts\n if(!is_home()){\n     show_related_post();\n }\n...\n}<\/code><\/pre>\n\n\n\n<p>Now open any single post and check sidebar. You will notice <strong>Related Posts<\/strong> widget above Recent Posts which will list all related posts from same category. I have also added a condition not to display related posts widget in the home page (as it does not make sense).<\/p>\n\n\n\n<p>If you liked this article or if you have any query, please leave your thoughts in the comment box below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a simple working way to display related posts from same category in sidebar without plugin in WordPress. For each post you can display related posts (of same category)&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[107,106,110,108,109],"class_list":["post-182","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-display-related-posts-in-sidebar-in-wordpress-without-plugin","tag-display-related-posts-in-wordpress-without-plugin","tag-related-posts-without-plugin","tag-wordpress-display-related-posts-from-same-category-without-plugin","tag-wp-related-posts-in-sidebar-without-plugin","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=182"}],"version-history":[{"count":21,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":351,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/182\/revisions\/351"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}