Archived topic
Remove excerpt from specific categories
46 replies · Started by Andy on June 17, 2019
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.
Hi there,
Can you try in_category()?
You can take a look at the WordPress conditional tag here:
https://codex.wordpress.org/Conditional_Tags
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;
}
}
Just checking, are you using custom excerpt? If not then the there is another filter you can use.
Let me know :)
I'm using the excerpt options in the Customizer > Layout > Blog > Excerpt Word count = 50
Hmm 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_Page
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.
I 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 ) ) {
}
Sorry Tom, so what would the full filter look like?.
Something 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;
}
}
That looked promising but still not working :s
Can you try the adjusted function above?
Sorry, still no change unfortunately.
Can you link me to a page where that condition should take effect?
Hi Tom,
I've edited my thread with a temporary public URL, it will only be live for 8hrs.
Thanks.