- This topic has 11 replies, 4 voices, and was last updated 3 years, 3 months ago by
Fernando.
-
AuthorPosts
-
June 27, 2022 at 7:25 pm #2266386
_blank
Hello,
I’m currently using WP Show Posts with the generate_after_content hook below to display 5 related posts underneath articles only, no images, no author, no date, no categories and randomly by TAG.
How can I do the same thing without WP Show Posts, please?
<div class="wpsp-related-posts grid-container"> <h3>Other tutorials that might interest you...</h3> <?php if ( is_single() ) { $tags = get_the_tags(); $tags_list = []; foreach ($tags as $tag) $tags_list[] = $tag->slug; $tag_string = implode( ', ', $tags_list); } else { $tag_string = get_tag( get_query_var( 'tag' ) ); } $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' ); wpsp_display( $list->ID, 'tax_term="' . $tag_string . '"' ); ?> </div>Thanks.
June 27, 2022 at 7:32 pm #2266392Fernando Customer Support
Hi Olivier,
You could use the new feature in GenerateBlocks called Query Loop Block.
Reference: https://docs.generateblocks.com/article/query-loop-overview/
Then, you’ll need to follow Tom’s instructions here to retrieve related Posts: https://community.generateblocks.com/t/use-gb-query-loop-for-related-post/688#:~:text=May%2027-,Hi%20there,-%2C
Hope this helps!
June 28, 2022 at 6:30 am #2266841_blank
Hello,
I don’t have anything displayed…
This one works: https://generatepress.com/forums/topic/how-can-i-display-a-list-of-post-after-my-content-ends/#post-2229282
But how do I add the H3 title, and display related items by tags randomly without the bulleted lists?
Thanks.
June 28, 2022 at 6:37 am #2266854David
StaffCustomer SupportHi there,
are you wanting each of the Post Titles to be a H3?
June 28, 2022 at 6:41 am #2266860_blank
Hello,
No just add the H3 title from the previous WP Show Posts code that I currently have.
<div class="wpsp-related-posts grid-container"> <h3>Other tutorials that might interest you...</h3> <?php if ( is_single() ) { $tags = get_the_tags(); $tags_list = []; foreach ($tags as $tag) $tags_list[] = $tag->slug; $tag_string = implode( ', ', $tags_list); } else { $tag_string = get_tag( get_query_var( 'tag' ) ); } $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' ); wpsp_display( $list->ID, 'tax_term="' . $tag_string . '"' ); ?> </div>June 28, 2022 at 6:55 am #2266870David
StaffCustomer SupportTry this PHP snippet:
function db_related_posts() { if ( ( is_single() && 'post' == get_post_type() ) ) { global $post; // Get the tags $tags = array(); $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { $tags[] = $tag->term_id; } } // 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 'tag__and' => $tags, ); $latest = new WP_Query($args); // Output loop if ( $latest->have_posts() ) { echo '<h3>Other tutorials that might interest you...</h3>'; 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');Then you can use some CSS to remove the bullets:
.related_post_list { list-style-type: none; margin-left: 0; }June 28, 2022 at 7:25 am #2266890_blank
Perfect, thanks David 😀
But it displays on WooCommerce product pages, how to display them only under the blog posts just?
June 28, 2022 at 7:49 am #2266905David
StaffCustomer SupportUpdated the code above 🙂
June 28, 2022 at 8:02 am #2267076_blank
Great, thanks a lot David 👌
June 29, 2022 at 1:08 am #2267744David
StaffCustomer SupportYou’re welcome
January 15, 2023 at 11:28 pm #2496945Praveen
Hi, I am looking to do sth like this ->
Background – Posts under the category “Country” that describe a country and have “country names” as one of the tags. There are other posts, under other categories, for example, best travel locations, they also have the same country name as tags. For example, Best travel Locations in Brazil will have “brazil” as tag.
Need: Is there a way to create a related post block using GB which I hook at end of *all posts of the category “Country”* and they automatically show posts from other categories which have tag which match the tag of the post. For example, under the profile of brazil, it shows, travel in brazil, food in brazil, places to visit in brazil etc. dynamically.
January 16, 2023 at 12:41 am #2497014Fernando Customer Support
Hi Praveen,
That should be possible with a GB Query Look Block. If you’ll need assistance with regard to this, kindly open a new topic.
Related posts should also be possible now with GB + GB Pro. See here for more info: https://generateblocks.com/creating-related-posts-with-query-loop-block/#:~:text=Related%20Posts%20with%20dynamic%20parameters
-
AuthorPosts
- You must be logged in to reply to this topic.