- This topic has 28 replies, 4 voices, and was last updated 2 years, 9 months ago by
David.
-
AuthorPosts
-
December 8, 2019 at 2:16 am #1097591
raysn
Hi there,
when I was searching for a solution how to implement a related posts area in GeneratePress, I found the following topic: https://generatepress.com/forums/topic/displaying-related-posts/
David suggested to use WP Show Posts (which I also got) together with the code he mentioned in https://gpsites.co/merch/the-single-post/. This is working great for category-related posts.
But I want to set tags as the related posts basis. So is there a way to modify Davids code snippet somehow that only posts with one or more matching tags are getting displayed instead of posts of the same category?
I tried to modify the code but didn’t get it work:
<div class="wpsp-related-posts grid-container"> <h2>Related Posts</h2> <?php if ( is_single() ) { $tags = get_the_tags(); $tag = $tags[0]; } else { $tag = get_tag( get_query_var( 'tag' ) ); } $tag_slug = $tag->slug; $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' ); wpsp_display( $list->ID, 'tax_term="' . $tag_slug . '"' ); ?> </div>
December 8, 2019 at 8:11 am #1097882David
StaffCustomer SupportHi there,
can you try editing your WPSP list and set the Taxonomy to
post_tag
but do not select any Terms. let me know.Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/December 8, 2019 at 3:54 pm #1098151raysn
Hi David,
thanks for your quick response!
I’ve changed the settings as you suggested, but unfortunately it still doesn’t work as it should, although at least a few posts are displayed now.
The related posts only refer to the first defined tag and all other tags are getting ignored (so less posts are shown). Do you have any idea how I can include all tags a post is tagged with?
December 10, 2019 at 4:10 am #1099658David
StaffCustomer SupportTry this:
<div class="wpsp-related-posts grid-container"> <h2>Related Posts</h2> <?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>
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/December 10, 2019 at 4:18 am #1099665raysn
This works fine for me.
Thank you very much!
December 10, 2019 at 4:48 am #1099690David
StaffCustomer SupportAwesome – that took a little while to figure out.
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/April 15, 2020 at 2:35 am #1239654Brian
Hi David
This works perfectly, but is it possible to take it one step further?
I would like it to show posts by tag, but if there is no tag, to should show posts from the category πApril 15, 2020 at 3:35 am #1239723David
StaffCustomer SupportHmmm… try this:
<div class="wpsp-related-posts grid-container"> <h2>Related Posts</h2> <?php if ( is_single() ) { // Get the Tags $tags = get_the_tags(); $tags_list = []; foreach ($tags as $tag) $tags_list[] = $tag->slug; $tags_term = implode( ', ', $tags_list); // Get the Categories $cats = get_the_category(); $cat = $cats[0]; $cat_term = $cat->slug; } // output Tags or Categories if ( has_tag() ) { $tax_term = $tags_term; } else { $tax_term = $cat_term; } // Display List $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' ); wpsp_display( $list->ID, 'tax_term="' . $tax_term . '"' ); ?> </div>
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/April 15, 2020 at 3:57 am #1239733Brian
That still shows no posts if there’s not a tag π
I guess I could make sure I give everything a tagApril 15, 2020 at 5:51 am #1239820David
StaffCustomer SupportIs the post in a Category ?
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/April 15, 2020 at 6:41 am #1239889Brian
Yes, all of the posts are in a category.
April 15, 2020 at 6:45 am #1239891Brian
Dave,
It works, when I was testing I had the show posts set as tag, sorry about that!So it’s all good now, thanks for the help, again π
April 15, 2020 at 6:51 am #1239904Brian
Sorry, that was a false dawn.
It now works if a post doesn’t have a tag and correctly shows posts from the category.
But doesn’t show anything if a post has a tag.April 15, 2020 at 8:17 am #1240263David
StaffCustomer SupportI edited the code above
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/April 15, 2020 at 8:56 am #1240350Brian
It now shows nothing if there is a tag, and shows posts from the category if there’s no tag.
-
AuthorPosts
- You must be logged in to reply to this topic.