- This topic has 46 replies, 3 voices, and was last updated 6 years, 9 months ago by
Tom.
-
AuthorPosts
-
June 21, 2019 at 3:01 am #936712
Andy
Hi Tom,
Did you get chance to have a look a this?. I’ve updated the thread with a new URL.June 21, 2019 at 8:46 am #937151Tom
Lead DeveloperLead DeveloperSorry! I didn’t get notified of your last reply for some reason.
So you’ve linked me to a category named “Advice” with three posts.
Should this category display the regular excerpt length for all posts, even the first one?
Where are you wanting to set that initial filter where the first post doesn’t have an excerpt? Maybe we can go at this a different way.
June 21, 2019 at 8:56 am #937166Andy
Hi Tom,
I don’t want any of the posts in the ‘Advice’ category to have the excerpt. That first post is showing the unwanted excerpt because it’s also in the ‘Buyers Guide’ sub-category of ‘Advice’.I have a ‘Routes’ category where I DO want the excerpts to show including all of its sub-categories.
Hope that all makes some sense.
June 21, 2019 at 3:15 pm #937462Tom
Lead DeveloperLead DeveloperSo, just so I have my logic right:
if post has category “advice” or has child category of “advice” category, show no excerpt.
else, show standard excerpt
Am I right?
June 21, 2019 at 3:36 pm #937475Andy
That’s correct!
June 22, 2019 at 8:36 am #937974Tom
Lead DeveloperLead DeveloperLet’s 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( 8 ) || post_is_in_descendant_category( 8 ) ) { return 0; } global $wp_query; // assuming you are using the main query if ( 0 === $wp_query->current_post) { return $length; } else { return 0; } }, 1000 );June 22, 2019 at 11:45 am #938082Andy
Sorry Tom, but now all the posts in the advice category are displaying excerpts.
I didn’t realise how difficult it would be to achieve this, as I thought targeting the category ID and sub-categories would be relatively straight forward, but obviously it isn’t!June 22, 2019 at 5:11 pm #938226Tom
Lead DeveloperLead DeveloperAnd
8is the right category ID? Instead ofhas_category, can you tryin_category()?June 22, 2019 at 5:34 pm #938232Andy
Just tried in_category but still no change, and double-checked Cat ID, its definitely 8.
Full code now as below:
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; } global $wp_query; // assuming you are using the main query if ( 0 === $wp_query->current_post) { return $length; } else { return 0; } } );June 23, 2019 at 7:38 am #938684Tom
Lead DeveloperLead DeveloperCan you try the updated code above?: https://generatepress.com/forums/topic/remove-excerpt-from-specific-categories/page/2/#post-937974
June 23, 2019 at 12:45 pm #938874Andy
At first I thought it was working as all the Advice posts weren’t displaying the excerpt, however when I checked posts in the Routes category they were also not displaying excerpts unfortunately.
I don’t really want to take up any more of your valuable time, so perhaps I should just hide them with CSS.
June 23, 2019 at 1:01 pm #938884Tom
Lead DeveloperLead DeveloperThat’s likely because of this part:
global $wp_query; // assuming you are using the main query if ( 0 === $wp_query->current_post) { return $length; } else { return 0; }There you’re saying: if it’s the first post, display the regular excerpt. Otherwise, don’t display an excerpt.
June 23, 2019 at 1:13 pm #938897Andy
Yes thats right the 1st post in the Route category was displaying the excerpt all the rest weren’t, how can we change that bit?
June 23, 2019 at 2:21 pm #938950Tom
Lead DeveloperLead DeveloperWhen is the above code ($wp_query) supposed to apply? On the main blog page? In a specific category?
June 23, 2019 at 2:59 pm #938976Andy
Ideally just on the Advice category archive page.
-
AuthorPosts
- You must be logged in to reply to this topic.