- This topic has 6 replies, 3 voices, and was last updated 4 years, 1 month ago by Satish.
-
AuthorPosts
-
February 25, 2020 at 4:50 am #1176107Marcel
Hi, is there a code available (to place in a hook) to show related posts based on tags? For example: If this post has the tag “trademark” 3 related post with the same tag should be displayed where you now see “Related articles” (in pink) on this page: https://entrepreneurcaribbean.com/2020/02/24/trademarks-as-the-entrepreneurs-value-creating-tool/.
Currently I’m using the WordPress Popular Posts plugin which makes it possible to do the same but the settings has to be done manually on every post.
Thanks for your help.
February 25, 2020 at 10:22 am #1176599LeoStaffCustomer SupportHi there,
Unfortunately there is nothing built-in in GP for this.
Related posts feature is actually quite complicated so best to handled by a plugin.
I’m not sure how WordPress Popular Posts plugin works but are you able to add it with hooks element so it can be displayed in all posts instead of adding it for each post?
Let me know ๐
February 25, 2020 at 10:41 am #1176625MarcelWould something like this work?
<?php //for use in the loop, list 5 post titles related to first tag on current post $tags = wp_get_post_tags($post->ID); if ($tags) { echo 'Related Posts'; $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'posts_per_page'=>5, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> <?php endwhile; } wp_reset_query(); } ?>
I found it on: https://www.wpbeginner.com/wp-tutorials/how-to-display-related-posts-in-wordpress/
February 25, 2020 at 10:56 am #1176648LeoStaffCustomer SupportI’m not sure. Feel free to give it a shot.
February 25, 2020 at 11:23 am #1176675MarcelI got it! This works:
<?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'=>3, // Number of related posts to display. 'caller_get_posts'=>1 ); $my_query = new wp_query( $args ); while( $my_query->have_posts() ) { $my_query->the_post(); ?> <ul class="wpp-list"> <li><a rel="external" href="<? the_permalink()?>"><?php the_title(); ?></a></li> </ul> <?php } } $post = $orig_post; wp_reset_query(); ?>
February 25, 2020 at 11:24 am #1176676LeoStaffCustomer SupportAwesome!
Thanks for reporting back.
I’ll keep in mind if someone else asks ๐
February 27, 2020 at 9:39 pm #1179323SatishHello Marcel,
Most of the generatepress layouts are not having any related posts feature. The developer removed the code because of speed improvement. He developed a special plugin called “WP Show Posts”. It has the shortcode feature to place the related posts anywhere within your blog by using theme inbuilt “HOOK” features. I am using this plugin for a few months. this plugin does not affect on theme and page loading speed. you can check in my blog post How to Add FAQ Schema.
-
AuthorPosts
- You must be logged in to reply to this topic.