- This topic has 46 replies, 3 voices, and was last updated 4 years, 9 months ago by Tom.
-
AuthorPosts
-
June 25, 2019 at 9:20 am #940822Andy
So is this going to be possible?
June 25, 2019 at 1:50 pm #941050TomLead DeveloperLead DeveloperI’m kind of confused. Isn’t
8
the Advice category? If so, aren’t we setting it to always have 0 excerpt?Try this:
if ( ! function_exists( 'post_is_in_descendant_category' ) ) { function post_is_in_descendant_category( $cats, $_post = null ) { foreach ( (array) $cats as $cat ) { // get_term_children() accepts integer ID only $descendants = get_term_children( (int) $cat, 'category' ); if ( $descendants && in_category( $descendants, $_post ) ) return true; } return false; } } add_filter( 'excerpt_length', function( $length ) { // If has category 8 or is child of category 8, show no excerpt; if ( in_category( 8 ) || post_is_in_descendant_category( 8 ) ) { return 0; } if ( is_category( 'Advice' ) ) { global $wp_query; // assuming you are using the main query if ( 0 === $wp_query->current_post) { return $length; } else { return 0; } } return $length; } );
June 25, 2019 at 2:55 pm #941095AndyYes thats correct, but with the previous function it was also affecting posts in the Routes category, leaving only the 1st post with an excerpt. This new function doesn’t remove any excerpts at all, sorry.
June 25, 2019 at 3:53 pm #941133TomLead DeveloperLead DeveloperThis new function shouldn’t change anything with the existing excerpts, it only adds a check for
is_category()
below where the other code should be taking effect.What happens if you remove this entire chunk of code?:
if ( is_category( 'Advice' ) ) { global $wp_query; // assuming you are using the main query if ( 0 === $wp_query->current_post) { return $length; } else { return 0; } }
June 25, 2019 at 4:11 pm #941141AndyThe same, excerpts displaying in all categories.
June 26, 2019 at 9:08 am #942046TomLead DeveloperLead DeveloperI’ll try to set this up on my test server and will let you know what I find ๐
June 26, 2019 at 1:33 pm #942288TomLead DeveloperLead DeveloperSo I set up two categories – one parent and one child of the parent. Then I added a post to the parent, and a post to the child.
Then I added this PHP:
if ( ! function_exists( 'post_is_in_descendant_category' ) ) { function post_is_in_descendant_category( $cats, $_post = null ) { foreach ( (array) $cats as $cat ) { // get_term_children() accepts integer ID only $descendants = get_term_children( (int) $cat, 'category' ); if ( $descendants && in_category( $descendants, $_post ) ) return true; } return false; } } add_filter( 'excerpt_length', function( $length ) { // If has category 8 or is child of category 8, show no excerpt; if ( has_category( 586 ) || post_is_in_descendant_category( 586 ) ) { return 0; } global $wp_query; // assuming you are using the main query if ( 0 === $wp_query->current_post) { return $length; } else { return 0; } }, 1000 );
My parent ID is
586
.Now, both of my posts do not have an excerpt (regardless of the archive I’m on).
June 26, 2019 at 2:25 pm #942320Andyok, now create a 3rd category, add 2 or more posts to that and see what happens with those.
June 26, 2019 at 6:56 pm #942424TomLead DeveloperLead DeveloperIs this a completely separate category, or a sub-category of the parent we’re targeting?
June 27, 2019 at 3:26 am #942670AndyCompletely separate category.
June 27, 2019 at 9:08 am #943056TomLead DeveloperLead DeveloperCan you try this?:
if ( ! function_exists( 'post_is_in_descendant_category' ) ) { function post_is_in_descendant_category( $cats, $_post = null ) { foreach ( (array) $cats as $cat ) { // get_term_children() accepts integer ID only $descendants = get_term_children( (int) $cat, 'category' ); if ( $descendants && in_category( $descendants, $_post ) ) return true; } return false; } } add_filter( 'excerpt_length', function( $length ) { // If has category 8 or is child of category 8, show no excerpt; if ( has_category( 586 ) || post_is_in_descendant_category( 586 ) ) { return 0; } return $length; }, 1000 );
June 27, 2019 at 9:27 am #943077AndyYou are THE man, working perfectly, thank you so much!. Were you able to reproduce the issue I was having?. All I need to do now is remove the ‘…read more’ link from the same category.
June 27, 2019 at 2:29 pm #943305TomLead DeveloperLead DeveloperThe function wasn’t returning
$length
at the end for posts that didn’t match a condition. Not sure how I missed it.You may need to hide the more link with CSS, but I can double-check if you can link me to the page again.
June 27, 2019 at 2:41 pm #943312AndyI’ve updated the thread with a newly generated URL which should last another 8hrs.
Many thanks again for this Tom, when funds allow it I will be sending you a donation!.
June 27, 2019 at 3:31 pm #943344TomLead DeveloperLead DeveloperLet’s hope this works a little faster than the last one..
add_filter( 'excerpt_more', function( $more ) { // If has category 8 or is child of category 8, show no excerpt; if ( has_category( 586 ) || post_is_in_descendant_category( 586 ) ) { return false; } return $more; }, 1000 );
Thanks! Happy to help ๐
-
AuthorPosts
- You must be logged in to reply to this topic.