-
Avadhut
Hi,
I want to create automatic Related posts after every post as per the category. How to do that?
Thanks.
-
Hi there,
You will need a plugin for this.
If you have GB Pro, you should be able to create simple related posts as well, here’s the doc for GB’s query loop block:
https://docs.generateblocks.com/article/use-query-loop-block-to-create-related-posts/ -
hotel1004
I am a PRO user. I would like to have related posts appear at the bottom of every post. Could I request more detailed instructions?
update)
Sorry, GB Pro?, I am GP Pro user… -
Hi there,
you would need to use a Related Posts plugin, as the to do this in GenerateBlocks requires the GenerateBlocks Pro plugin
-
hotel1004
Thank you David,
I obtained the following code through web surfing, but there is a bug that retrieves posts from all categories instead of just the same category. Is it possible to fix this?
<div class="related-posts"> <h4><span><?php $categories = get_the_category(); echo esc_html( $categories[0]->name ); ?></span> Related Posts/h4> <?php $post_id = get_the_ID(); $cat_ids = array(); $categories = get_the_category( $post_id ); if(!empty($categories) && is_wp_error($categories)): foreach ($categories as $category): array_push($cat_ids, $category->term_id); endforeach; endif; $current_post_type = get_post_type($post_id); $query_args = array( 'category__in' => wp_get_post_categories($post->ID), 'post_type' => $current_post_type, 'post__not_in' => array(get_the_ID()), 'posts_per_page' => '5', 'orderby' => 'rand' ); $related_cats_post = new WP_Query( $query_args ); if($related_cats_post->have_posts()): while($related_cats_post->have_posts()): $related_cats_post->the_post(); ?> <ul> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </li> </ul> <?php endwhile; // Restore original Post Data wp_reset_postdata(); endif; ?> </div> -
Fernando
Hi there,
Sorry, but that would be out of our scope. Quickly glancing at the code, try replacing
$post->IDwith$post_id.Otherwise, if this doesn’t work, our recommendation would be to use GB Pro for this.
- You must be logged in to reply to this topic.