Site logo

[Resolved] Remove excerpt from specific categories

Home Forums Support [Resolved] Remove excerpt from specific categories

Home Forums Support Remove excerpt from specific categories

Viewing 15 posts - 16 through 30 (of 47 total)
  • Author
    Posts
  • #936712
    Andy

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

    #937151
    Tom
    Lead Developer
    Lead Developer

    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.

    #937166
    Andy

    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.

    #937462
    Tom
    Lead Developer
    Lead Developer

    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?

    #937475
    Andy

    That’s correct!

    #937974
    Tom
    Lead Developer
    Lead Developer

    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 );
    #938082
    Andy

    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!

    #938226
    Tom
    Lead Developer
    Lead Developer

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

    #938232
    Andy

    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;
        }
    
    } );
    #938684
    Tom
    Lead Developer
    Lead Developer
    #938874
    Andy

    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.

    #938884
    Tom
    Lead Developer
    Lead Developer

    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.

    #938897
    Andy

    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?

    #938950
    Tom
    Lead Developer
    Lead Developer

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

    #938976
    Andy

    Ideally just on the Advice category archive page.

Viewing 15 posts - 16 through 30 (of 47 total)
  • You must be logged in to reply to this topic.