[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 - 31 through 45 (of 47 total)
  • Author
    Posts
  • #940822
    Andy

    So is this going to be possible?

    #941050
    Tom
    Lead Developer
    Lead Developer

    I’m kind of confused. Isn’t 8 the Advice category? If so, aren’t we setting it to always have 0 excerpt?

    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 ( in_category( 8 ) || post_is_in_descendant_category( 8 ) ) {
            return 0;
        }
    
        if ( is_category( 'Advice' ) ) {
            global $wp_query; // assuming you are using the main query
    
            if ( 0 === $wp_query->current_post) {
                return $length;
            } else {
                return 0;
            }
        }
    
        return $length;
    
    } );
    #941095
    Andy

    Yes thats correct, but with the previous function it was also affecting posts in the Routes category, leaving only the 1st post with an excerpt. This new function doesn’t remove any excerpts at all, sorry.

    #941133
    Tom
    Lead Developer
    Lead Developer

    This new function shouldn’t change anything with the existing excerpts, it only adds a check for is_category() below where the other code should be taking effect.

    What happens if you remove this entire chunk of code?:

    if ( is_category( 'Advice' ) ) {
        global $wp_query; // assuming you are using the main query
    
        if ( 0 === $wp_query->current_post) {
            return $length;
        } else {
            return 0;
        }
    }
    #941141
    Andy

    The same, excerpts displaying in all categories.

    #942046
    Tom
    Lead Developer
    Lead Developer

    I’ll try to set this up on my test server and will let you know what I find ๐Ÿ™‚

    #942288
    Tom
    Lead Developer
    Lead Developer

    So I set up two categories – one parent and one child of the parent. Then I added a post to the parent, and a post to the child.

    Then I added this PHP:

    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( 586 ) || post_is_in_descendant_category( 586 ) ) {
            return 0;
        }
    
        global $wp_query; // assuming you are using the main query
    
        if ( 0 === $wp_query->current_post) {
            return $length;
        } else {
            return 0;
        }
    
    }, 1000 );

    My parent ID is 586.

    Now, both of my posts do not have an excerpt (regardless of the archive I’m on).

    #942320
    Andy

    ok, now create a 3rd category, add 2 or more posts to that and see what happens with those.

    #942424
    Tom
    Lead Developer
    Lead Developer

    Is this a completely separate category, or a sub-category of the parent we’re targeting?

    #942670
    Andy

    Completely separate category.

    #943056
    Tom
    Lead Developer
    Lead Developer

    Can you 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( 586 ) || post_is_in_descendant_category( 586 ) ) {
            return 0;
        }
    
        return $length;
    
    }, 1000 );
    #943077
    Andy

    You are THE man, working perfectly, thank you so much!. Were you able to reproduce the issue I was having?. All I need to do now is remove the ‘…read more’ link from the same category.

    #943305
    Tom
    Lead Developer
    Lead Developer

    The function wasn’t returning $length at the end for posts that didn’t match a condition. Not sure how I missed it.

    You may need to hide the more link with CSS, but I can double-check if you can link me to the page again.

    #943312
    Andy

    I’ve updated the thread with a newly generated URL which should last another 8hrs.

    Many thanks again for this Tom, when funds allow it I will be sending you a donation!.

    #943344
    Tom
    Lead Developer
    Lead Developer

    Let’s hope this works a little faster than the last one..

    add_filter( 'excerpt_more', function( $more ) {
    
        // If has category 8 or is child of category 8, show no excerpt;
        if ( has_category( 586 ) || post_is_in_descendant_category( 586 ) ) {
            return false;
        }
    
        return $more;
    
    }, 1000 );

    Thanks! Happy to help ๐Ÿ™‚

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