- This topic has 11 replies, 4 voices, and was last updated 3 years, 6 months ago by
David.
-
AuthorPosts
-
May 21, 2022 at 2:26 am #2227474
Sohaib
Actually, I am building a fairly large website that might entail 1000+ pages so I want to interlink them all.
The latest post list is a solution but it will only display posts that are recently published.
I want a post display list of random posts in my category and so most of my pages are interlined this way.
Can you guys help me in this regard? Thanks
May 21, 2022 at 10:23 am #2227904Leo
StaffCustomer SupportHi there,
You will need a third-party plugin for this.
Maybe a related posts plugin?
https://en-ca.wordpress.org/plugins/contextual-related-posts/You could also consider the query loop block in GB 1.5 which is currently in Beta:
https://generateblocks.com/generateblocks-1-5-dynamic-data-query-loops-image-blocks/May 23, 2022 at 2:09 am #2229223Sohaib
I want to generate a list like this, I can’t seem to do it with a query loop. can you help me in this regard. thanks
May 23, 2022 at 3:15 am #2229282David
StaffCustomer SupportHi there,
try adding this PHP Snippet:
function db_related_posts() { global $post; // Set query args $args = array( 'post_type' => 'post', 'posts_per_page' => '3', 'orderby' => 'rand', 'post__not_in' => array( $post->ID ), // don't display current post ); // Optional arguments for setting category term relationship on single post if ( is_single() && has_category() ) { $category = get_the_category($post->ID); $category_id = $category[0]->cat_ID; $category_count = $category[0]->count; if ( $category_count > 1 ) { $args['category__in'] = array($category_id); } } $latest = new WP_Query($args); // Output loop if ( $latest->have_posts() ) { echo '<ul class="related_post_list">'; while ($latest->have_posts()) : $latest->the_post(); ?> <li class="related-post"> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> </li> <?php endwhile; echo '</li>'; wp_reset_postdata(); } } add_action('generate_after_content', 'db_related_posts');May 26, 2022 at 5:30 am #2233290Sohaib
where do I add this code ?
May 26, 2022 at 5:56 am #2233319David
StaffCustomer SupportThis article explains how to add PHP:
May 27, 2022 at 3:26 am #2234980Sohaib
okay so now the code is added and working perfectly but it’s also showing everywhere on the website. I just want to show it on posts only, not pages and homepage as well.
can you help me in this regard? thanks.
May 27, 2022 at 3:49 am #2235003David
StaffCustomer SupportOk.
Remove this line from the code:add_action('generate_after_content', 'db_related_posts');and add this instead:
add_action('wp',function(){ if ( is_single() ) { add_action('generate_after_content', 'db_related_posts'); } });May 27, 2022 at 4:08 am #2235015Sohaib
It’s done!
Well, I have to say David thanks a ton mate to you and the whole team of Generate Press.
You guys are doing a tremendous job in helping out people with their problems. God Bless!
May 27, 2022 at 4:37 am #2235057David
StaffCustomer SupportAwesome – glad to hear that, and thank you for the kind words – it is much appreciated 🙂
October 9, 2022 at 5:15 am #2367643Anjali Chawla
Hi David,
I want to display the related posts with featured images and post dates after every single post.
something like this: https://prnt.sc/l2_ErmbBquGj
I tried query loop but I couldn’t use it the way I want because I don’t have GenerateBlocks Premium. I don’t have certain parameter values like the current post, current post terms, and current post author. Can you provide me a PHP snippet for displaying the related posts in the format I want?
Thanks
October 9, 2022 at 6:10 am #2367711David
StaffCustomer SupportHi there,
Tom provides some code in the GB Community – see here:
https://community.generateblocks.com/t/use-gb-query-loop-for-related-post/688/2
-
AuthorPosts
- You must be logged in to reply to this topic.