Archived topic
How could I build related post section like this?
37 replies · Started by kiant123 on August 31, 2021
Hi,
That worked!!
I don't understand anything in that code you wrote but I've decided to add a couple more tag IDs in but have no idea how I would add them to the code. I would like to add '161' and '220'.
Also, does the code work in such a way that if there aren't any tags found then the whole related posts section won't show? I am hoping it does work this way.
Thanks again!
I updated the code here - so it checks first if the post has_tags and included a check for that other ID:
Sorry to be a pain, but i'd also like ID 161 added in there too please so there are three tag IDs. I was going to try to add it myself but would probably fail and mess everything up!
:) Added it to the code!
I put in the updated code and it made my site angry and it broke again like before (see screenshot). Thanks.
Damnit - sorry about that. revised the code to remove the schoolboy mistake!
Unfortunately, my site still doesn't like it! Still breaking down.
Oh my... i think i am having one of those days. Updated again... i double tested the code.... fingers crossed.
What...??? is it me??? It's happening again. Am I copying the right code? From here?
Thats the correct code. And i double checked it.
Remove your existing code, in case theres a rogue character in there and then copy and paste that code again.
This still isn't working for me, unless I am copying the code from the wrong place.
You're welcome to try yourself as I could be making some stupid mistake on my end. I've left the code area blank for now.
Hello, is there anyone out there?
Sorry i though i had replied yesterday, theres a chance the server timed out.
I tested the last code i posted and that causes no errors on development site.
Can you try pasting that again.
If its not excluding the three tags, then we may have just hit a logic wall, in regards to having posts with multiple tags trying to be compared.
Yes, it doesn't seem to work right with the three tags, so I did delete the two in the code (220 & 161) to try to put the code the way it was in the first place (with just 162) but it's not working quite right because no related posts show up for the URL i've added. That URL should show some related posts as it has no tags that should be hiding.
Would it be possible to get the code for when I was just blocking 162?
Try:
<div class="wpsp-related-posts1 grid-container">
<h2>Related Posts</h2>
<?php
$tax = 'post_tag';
$tax_term = '';
if ( is_single() ) {
$tags = get_the_tags();
$tags_list = [];
$tag_ids = [];
foreach ( (array) $tags as $tag) {
if ( isset( $tag->slug ) && 162 != $tag->term_id ) {
$tags_list[] = $tag->slug;
$tag_ids[] = $tag->term_id;
}
}
$args = array(
'numberposts' => 5,
'tag__in' => $tag_ids,
'post__not_in' => array( get_the_ID() )
);
$posts = get_posts( $args );
if ( ! empty( $posts ) && ! empty( $tag_ids ) ) {
$tax_term = implode( ', ', $tags_list);
} else {
$cats = get_the_category();
if ( ! empty( $cats ) ) {
$tax_term = $cats[0]->slug;
}
$tax = 'category';
}
}
$list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
wpsp_display( $list->ID, 'taxonomy=' . $tax . '&tax_term=' . $tax_term );
?>
</div>