Site logo

Archived topic

Remove excerpt from specific category

3 replies · Started by Dave on July 2, 2019

Viewing posts 1–4 of 4

I’m using an excerpt I found on this forum to hide the excerpt from a specific category:

add_filter( 'excerpt_length','tu_custom_excerpt_length', 1000 );
function tu_custom_excerpt_length( $length ) 
{
    // Return the normal length if we're not on the category
    if ( ! is_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_more', 'tu_custom_more_tag', 100 );
function tu_custom_more_tag( $more ) 
{
    // Return the normal more link if we're not on the category
    if ( ! is_category( '8' ) )
        return $more;

    global $wp_query; // assuming you are using the main query
    if ( 0 === $wp_query->current_post) {
        return $more;
    } else {
        return '';
    }
}

This is working, except the excerpt for the first item in that category still displays the excerpt. Any ideas what might be causing that?

So right now, that code is doing this:

If we're not in category 8, show the regular excerpt.

If we're in category 8, sho the regular excerpt for the first post, and no excerpt for the others.

So from what I can see, those functions aren't removing the excerpt from a specific category.

If that's what you want to do, you need to do this:

add_filter( 'excerpt_length', function( $length ) {
    if ( is_category( 10 ) ) {
        return 0;
    }

    return $length;
}, 1000 );

add_filter( 'excerpt_more', function( $more ) {
    if ( is_category( 10 ) ) {
        return '';
    }

    return $more;
}, 1000 );

That’s worked perfectly – thanks so much!

You're welcome :)

This archived topic is closed to new replies.