[Resolved] Remove excerpt from specific category

Home Forums Support [Resolved] Remove excerpt from specific category

Home Forums Support Remove excerpt from specific category

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #946769
    Dave

    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?

    #947193
    Tom
    Lead Developer
    Lead Developer

    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 );
    #947327
    Dave

    That’s worked perfectly – thanks so much!

    #947451
    Tom
    Lead Developer
    Lead Developer

    You’re welcome 🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.