- This topic has 41 replies, 2 voices, and was last updated 5 years, 6 months ago by
Elvin.
-
AuthorPosts
-
May 11, 2020 at 10:51 am #1279541
Christian
Hello, GP Team!
I’m currently using GP Hooks and WP Show Posts Pro to display related posts by Tags, however, there are a few Tags that only have 1 post, which makes the code I’m using return the following message:
“Sorry, no more posts to show”
I was wondering if it was possible to make it so that if there are no more posts in that Tag, to make it show posts in that Category instead.
I’m using a Hook code found in another GP thread. Here it is:
<div class="wpsp-related-posts1 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>Thanks in advance ๐
May 11, 2020 at 5:25 pm #1279988Tom
Lead DeveloperLead DeveloperHi there,
In your list settings, is the taxonomy set to post_tag?
We may be able to adjust the settings a bit in the PHP.
Let me know ๐
May 11, 2020 at 5:42 pm #1280006Christian
Hey, Tombeans!
Yes, the taxonomy in my list settings is set to post_tag. ๐
May 12, 2020 at 10:09 am #1281194Tom
Lead DeveloperLead DeveloperOk, let’s try this:
<div class="wpsp-related-posts1 grid-container"> <h2>Related Posts</h2> <?php $tax = 'post_tag'; if ( is_single() ) { $tags = get_the_tags(); $tags_list = []; $tag_ids = []; foreach ($tags as $tag) { $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 ) ) { $tax_term = implode( ', ', $tags_list); } else { $cats = get_the_category(); $text_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>May 12, 2020 at 11:03 am #1281289Christian
Hey, Tom!
Nope, it’s not working ๐
May 12, 2020 at 4:21 pm #1281712Tom
Lead DeveloperLead DeveloperThat’s a shame. Can you try the updated code?: https://generatepress.com/forums/topic/related-posts-by-tags-and-categories-wp-show-posts-pro-gp-hooks/#post-1281194
If that doesn’t work we’ll likely need to add some debugging stuff in there to see what’s going on.
May 12, 2020 at 10:31 pm #1281914Christian
Hey, Tom the bomb!
I tried the updated one, but unfortunately, it didn’t work. I went ahead and deactivated Autoptimize to see if it was maybe some setting there, but nothing changed. ๐ ๐
May 13, 2020 at 9:15 am #1282802Tom
Lead DeveloperLead DeveloperAre the tags still working if they exist?
May 14, 2020 at 7:50 am #1284293Christian
Yes, with the updated code, if the Tag has more than 1 blog post, it still works and shows the related posts.
Not sure if that’s what you meant by “exist?” ๐
May 14, 2020 at 10:06 am #1284580Tom
Lead DeveloperLead DeveloperOk, let’s debug:
<div class="wpsp-related-posts1 grid-container"> <h2>Related Posts</h2> <?php $tax = 'post_tag'; if ( is_single() ) { $tags = get_the_tags(); $tags_list = []; foreach ($tags as $tag) { $tags_list[] = $tag->slug; } var_dump($tags_list); if ( $tags_list ) { $tax_term = implode( ', ', $tags_list); } else { $cats = get_the_category(); $text_term = $cats[0]->slug; $tax = 'category'; } } else { $tax_term = get_tag( get_query_var( 'tag' ) ); if ( ! $tax_term ) { $cat = get_category( get_query_var( 'cat' ) ); if ( $cat ) { $text_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>What outputs on a post that should fall back to the category?
May 14, 2020 at 10:46 am #1284635Christian
Here’s what outputs on posts that should fall back to the category:
array(1) { [0]=> string(21) “nutrition-supplements” }
Sorry, no posts were found.May 14, 2020 at 7:58 pm #1285047Tom
Lead DeveloperLead DeveloperAh, so we need a separate posts query to see if any posts exist.
Maybe this?: https://generatepress.com/forums/topic/related-posts-by-tags-and-categories-wp-show-posts-pro-gp-hooks/#post-1281194
May 14, 2020 at 8:14 pm #1285068Christian
Nope, didn’t work. ๐ ๐
May 15, 2020 at 9:38 am #1285989Tom
Lead DeveloperLead Developer<div class="wpsp-related-posts1 grid-container"> <h2>Related Posts</h2> <?php $tax = 'post_tag'; if ( is_single() ) { $tags = get_the_tags(); $tags_list = []; $tag_ids = []; foreach ($tags as $tag) { $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 ) ) { $tax_term = implode( ', ', $tags_list); } else { $cats = get_the_category(); $text_term = $cats[0]->slug; $tax = 'category'; } } var_dump('taxonomy=' . $tax . '&tax_term=' . $tax_term); $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' ); wpsp_display( $list->ID, 'taxonomy=' . $tax . '&tax_term=' . $tax_term ); ?> </div>What gets output here when no posts exist?
May 15, 2020 at 10:05 am #1286049Christian
This is what gets output when no posts exist:
array(1) { [0]=> object(WP_Post)#2457 (24) { [“ID”]=> int(5621) [“post_author”]=> string(1) “3” [“post_date”]=> string(19) “2019-09-22 15:21:40” [“post_date_gmt”]=> string(19) “2019-09-22 19:21:40” [“post_content”]=> string(10243)
Plus, the whole current blog post. ๐
-
AuthorPosts
- You must be logged in to reply to this topic.