- This topic has 46 replies, 3 voices, and was last updated 6 years, 9 months ago by
Tom.
-
AuthorPosts
-
June 17, 2019 at 8:08 am #932405
Andy
Hi,
I’m trying to remove the excerpt from specific post categories but leave them enabled on the rest of the site.I tried tweaking Tom’s code below:
add_filter( 'excerpt_length','tu_custom_excerpt_length', 1000 ); function tu_custom_excerpt_length( $length ) { // Return the normal length if we're not in this category if ( ! is_category('advice') ) return $length; global $wp_query; // assuming you are using the main query if ( 0 === $wp_query->current_post) { return $length; } else { return 0; } }The code works, however, if there are posts from sub-categories such as advice > reviews then they are still showing the excerpt
Any help much appreciated.
June 17, 2019 at 8:29 am #932443Leo
StaffCustomer SupportHi there,
Can you try
in_category()?You can take a look at the WordPress conditional tag here:
https://codex.wordpress.org/Conditional_TagsJune 17, 2019 at 8:43 am #932457Andy
Yeh, I had tried both of the below using in_category but it still shows the excerpt on posts from the sub-category (id=5).
add_filter( 'excerpt_length','tu_custom_excerpt_length', 1000 ); function tu_custom_excerpt_length( $length ) { // Return the normal length if we're not in this category if ( ! in_category('8') ) return $length; global $wp_query; // assuming you are using the main query if ( 0 === $wp_query->current_post) { return $length; } else { return 0; } }add_filter( 'excerpt_length','tu_custom_excerpt_length', 1000 ); function tu_custom_excerpt_length( $length ) { // Return the normal length if we're not in this category if ( ! in_category( array( 8,5 ) )) return $length; global $wp_query; // assuming you are using the main query if ( 0 === $wp_query->current_post) { return $length; } else { return 0; } }June 17, 2019 at 11:17 am #932595Leo
StaffCustomer SupportJust checking, are you using custom excerpt? If not then the there is another filter you can use.
Let me know 🙂
June 17, 2019 at 11:23 am #932601Andy
I’m using the excerpt options in the Customizer > Layout > Blog > Excerpt Word count = 50
June 17, 2019 at 11:53 am #932622Leo
StaffCustomer SupportHmm can you try the conditional tag:
is_category( 5 ) || cat_is_ancestor_of( 5, get_query_var( 'cat' ) )
as suggested here?
https://codex.wordpress.org/Conditional_Tags#A_Category_PageJune 17, 2019 at 12:01 pm #932627Andy
I’d actually also tried various versions of the below with no luck:
add_filter( 'excerpt_length','tu_custom_excerpt_length', 1000 ); function tu_custom_excerpt_length( $length ) { // Return the normal length if we're not in this category if ( ! is_category( 8 ) || cat_is_ancestor_of( 8, get_query_var( 'advice' ) )) return $length; global $wp_query; // assuming you are using the main query if ( 0 === $wp_query->current_post) { return $length; } else { return 0; } }Was hoping Tom could take a look.
June 17, 2019 at 2:47 pm #932750Tom
Lead DeveloperLead DeveloperI wonder if this function would be better: https://codex.wordpress.org/Function_Reference/get_ancestors
Your check would be:
$ancestor_categories = get_ancestors( get_query_var( 'advice' ), 'category' ); if ( ! in_array( 8, $ancestor_categories ) ) { }June 17, 2019 at 3:26 pm #932773Andy
Sorry Tom, so what would the full filter look like?.
June 18, 2019 at 5:53 am #933239Tom
Lead DeveloperLead DeveloperSomething like this maybe?:
add_filter( 'excerpt_length','tu_custom_excerpt_length', 1000 ); function tu_custom_excerpt_length( $length ) { $ancestor_categories = get_ancestors( get_query_var( 'advice' ), 'category' ); // Return the normal length if we're not in this category if ( ! is_category( 8 ) && ! in_array( 8, $ancestor_categories ) ) return $length; global $wp_query; // assuming you are using the main query if ( 0 === $wp_query->current_post) { return $length; } else { return 0; } }June 18, 2019 at 2:47 pm #933916Andy
That looked promising but still not working :s
June 18, 2019 at 3:30 pm #933947Tom
Lead DeveloperLead DeveloperCan you try the adjusted function above?
June 18, 2019 at 3:56 pm #933951Andy
Sorry, still no change unfortunately.
June 19, 2019 at 5:12 am #934451Tom
Lead DeveloperLead DeveloperCan you link me to a page where that condition should take effect?
June 19, 2019 at 5:58 am #934507Andy
Hi Tom,
I’ve edited my thread with a temporary public URL, it will only be live for 8hrs.Thanks.
-
AuthorPosts
- You must be logged in to reply to this topic.