Home › Forums › Pro Support › Related Post
- This topic has 40 replies, 3 voices, and was last updated 2 years, 2 months ago by
Tom.
-
AuthorPosts
-
November 4, 2020 at 3:17 pm #22223
Tom
KeymasterLet’s try this:
<?php if ( is_single() ) { $cat_values = array(); $cats = get_the_category(); foreach ( $cats as $cat ) { $cat_values[] = $cat->slug; } } else { $cat_values = get_category( get_query_var( 'cat' ) ); } // Remove first category, assuming it's a parent. if ( count( $cat_values ) > 1 ) { unset( $cat_values[0] ); } $cat_slug = implode( ', ', $cat_values ); $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' ); wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' ); ?>
November 4, 2020 at 3:36 pm #22225Samuel
ParticipantIt seems to work ! 🙂 WPSP is displaying child category on post with child category and when post has no child category it’s displaying the category.
The only thing really weird is i got just one category and its child category that is displaying a “Sorry, no posts were found.”
November 6, 2020 at 2:19 pm #22275Tom
KeymasterCan you explain that a bit more? Not sure I understand.
November 7, 2020 at 3:32 am #22293Samuel
ParticipantSorry about that, it shows “no posts were found” because there is only one post inside this category… so it’s normal !
To go further, I’m showing 4 related posts : Would it possible to tell WPSP to show other posts from parent category or from other category if there is less than 4 posts in the current category ?
November 9, 2020 at 2:47 pm #22402Tom
KeymasterYou could try this:
<?php if ( is_single() ) { $cat_values = array(); $cats = get_the_category(); foreach ( $cats as $cat ) { $cat_values[] = $cat->slug; } } else { $cat_values = get_category( get_query_var( 'cat' ) ); } // Remove first category as long as there's more than 4 categories. if ( count( $cat_values ) > 4 ) { unset( $cat_values[0] ); } $cat_slug = implode( ', ', $cat_values ); $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' ); wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' ); ?>
November 10, 2020 at 2:56 am #22451Samuel
ParticipantHi Tom,
Here was the first code that was resolving the issue of “Display Child Category, and if there is not then display Parent Category” :
// Remove first category, assuming it's a parent. if ( count( $cat_values ) > 1 ) { unset( $cat_values[0] ); }
If as you suggest I replace it with :
// Remove first category as long as there's more than 4 categories. if ( count( $cat_values ) > 4 ) { unset( $cat_values[0] ); }
It’s displaying parent category everywhere, so first issue is coming back and it’s not displaying Child category when there is one.
What would be awesome is :
1 / Priority is “Get Child Category”
2 / Then “If there is no Child Category then Get Category”
3 / And “If there is less then 4 items in a Child Category then get Category”Would that be possible ?
November 10, 2020 at 3:27 pm #22474Tom
KeymasterWhat that code should do is remove the parent category if there are more than 4 categories to pull posts from.
So if you have this:
– Parent
– Child
– Child
– ChildThen it will get posts from the parent.
However, if you have this:
– Parent
– Child
– Child
– Child
– ChildIt will only pull posts from the children.
November 11, 2020 at 6:12 am #22497Samuel
ParticipantOk, so there is a misunderstanding.
I’m showing 4 related posts and I was asking if it’s possible to show posts from another category or from a parent category in the case there is less then 4 posts in a child category.
Don’t know if this make sense…
November 11, 2020 at 3:37 pm #22518Tom
KeymasterAh sorry, I understand now. That gets even more complex as you need to count the posts in each category that’s being rendered in that function. Unfortunately, we’ve already gone a little out of the scope of support in this forum – something like this would take quite a lot of tweaking and playing with in order to get it working correctly.
November 12, 2020 at 2:18 am #22527Samuel
ParticipantOk, I understand 🙂 Thanks a lot Tom !
November 12, 2020 at 8:26 pm #22564Tom
KeymasterNo problem!
-
AuthorPosts
- You must be logged in to reply to this topic.