Site logo

Archived topic

Remove excerpt from specific categories

46 replies · Started by Andy on June 17, 2019

Viewing posts 16–30 of 47

Hi Tom,
Did you get chance to have a look a this?. I've updated the thread with a new URL.

Sorry! 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.

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.

So, 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?

That's correct!

Let'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 );

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!

And 8 is the right category ID? Instead of has_category, can you try in_category()?

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;
    }

} );

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.

That'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.

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?

When is the above code ($wp_query) supposed to apply? On the main blog page? In a specific category?

Ideally just on the Advice category archive page.

This archived topic is closed to new replies.