Site logo

Archived topic

Remove excerpt from specific categories

46 replies · Started by Andy on June 17, 2019

Viewing posts 31–45 of 47

So is this going to be possible?

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;

} );

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.

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

The same, excerpts displaying in all categories.

I'll try to set this up on my test server and will let you know what I find :)

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).

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

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

Completely separate category.

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

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.

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.

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

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 :)

This archived topic is closed to new replies.