Archived topic
Related Posts by Post Tags
28 replies · Started by raysn on December 8, 2019
Hey 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 :)
Still shows nothing if there is a tag, but shows posts from the category if there’s no tag :(
Weird. 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;
}
If I remove that it shows no posts at all.
I 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?
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.
I can only assume there is something else effecting that function on your site. Which makes it fairly impossible for us to help in debugging.
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.
If 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.
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.
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>
I believe the code the OP posted originally ( click here) worked with the post_tag taxonomy being selected in the WPSP list.
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.
You're welcome - thanks for the heads up