- This topic has 28 replies, 4 voices, and was last updated 6 years, 1 month ago by
David.
-
AuthorPosts
-
April 15, 2020 at 8:25 pm #1240915
Tom
Lead DeveloperLead DeveloperHey Brian,
How about 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' ) ); } if ( ! $tag_string ) { $cats = get_the_category(); $cat = $cats[0]; $tag_string = $cat->slug; } $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' ); wpsp_display( $list->ID, 'tax_term="' . $tag_string . '"' ); ?> </div>Let me know 🙂
April 16, 2020 at 7:03 am #1241395Brian
Still shows nothing if there is a tag, but shows posts from the category if there’s no tag 🙁
April 16, 2020 at 3:34 pm #1242200Tom
Lead DeveloperLead DeveloperWeird. If you remove the category part (below), does it display based on tags?:
if ( ! $tag_string ) { $cats = get_the_category(); $cat = $cats[0]; $tag_string = $cat->slug; }April 17, 2020 at 1:41 am #1242532Brian
If I remove that it shows no posts at all.
April 17, 2020 at 7:05 am #1242889David
StaffCustomer SupportI tested the different code options and they are all doing pretty much the same thing. So using Tom’s latest version
1. All my posts had only Categories, NO tags. And the related posts worked like the original merch site did showing related categories. Thats correct.
2. Now i edited two of my posts and gave them both the same tag. For these two posts – the related posts just shows me the other post. Thats correct.
3. Now the issue. If i add a post that has a unique tag, not shared by any other post, the Related Posts displays Nothing Found. Which is actually correct as we’re not testing if there are more than 1 post with the same tag in our fallback condition.
Can you check if this applies on your site?
April 17, 2020 at 8:59 am #1243176Brian
Hi David, This doesn’t apply to my site as I only have tags that have at least a few posts. So to clarify my situation, all posts have a category and most also have a tag. The posts with category and tag are not showing related.
If you cant duplicate the error then I guess I must have a conflict of some sort. I’m thinking that I could make sure all posts have a tag (which is no-index so shouldn’t cause issues) to overcome this.
April 18, 2020 at 3:40 am #1243960David
StaffCustomer SupportI can only assume there is something else effecting that function on your site. Which makes it fairly impossible for us to help in debugging.
April 18, 2020 at 6:19 am #1244068Brian
FYI, As a last attempt, I tried this with all plugins switched off except GP Premium, Show Posts, and Simple CSS
I still got nothing when there was a tag.April 18, 2020 at 7:19 am #1244141David
StaffCustomer SupportIf there are no other functions that could be conflicting then maybe the last thing to check is the WP Show Posts List you have set up. Make sure there are no Taxonomies defined or other changes made in the More Settings.
April 18, 2020 at 8:05 am #1244340Brian
I’ve tried it with and without taxonomies set and there’s nothing in the More settings apart from excluding current. (I tried that off as well), perhaps I’m missing something obvious. But at this point I think I will go ahead and use tags, making sure I give all posts a tag. Thanks for all the help guys.
April 18, 2020 at 8:47 am #1244389Brian
So I have one final question, sorry 🙂
I have my Posts type set as tags, but with no Taxonomy selected.
I’m using the code below which works to show posts. But if a post has multiple tags it would help if we could edit this to show the first tag only, as I need to narrow it down, is that possible?<div class="wpsp-related-posts grid-container"> <h4>Read More About This</h4> <?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>April 19, 2020 at 2:14 am #1245023David
StaffCustomer SupportI believe the code the OP posted originally ( click here) worked with the
post_tagtaxonomy being selected in the WPSP list.April 19, 2020 at 8:52 am #1245518Brian
Thanks David, that worked.
FYI, it shows posts from the last tag created (not last assigned in post editor), just in case you weren’t aware of that.April 20, 2020 at 1:01 am #1246176David
StaffCustomer SupportYou’re welcome – thanks for the heads up
-
AuthorPosts
- You must be logged in to reply to this topic.