[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 - 1 through 15 (of 47 total)
  • Author
    Posts
  • #932405
    Andy

    Hi,
    I’m trying to remove the excerpt from specific post categories but leave them enabled on the rest of the site.

    I tried tweaking Tom’s code below:

    add_filter( 'excerpt_length','tu_custom_excerpt_length', 1000 );
    function tu_custom_excerpt_length( $length ) 
    {
        // Return the normal length if we're not in this category
        if ( ! is_category('advice') )
            return $length;
    
        global $wp_query; // assuming you are using the main query
        if ( 0 === $wp_query->current_post) {
            return $length;
        } else {
            return 0;
        }
    }

    The code works, however, if there are posts from sub-categories such as advice > reviews then they are still showing the excerpt

    Any help much appreciated.

    #932443
    Leo
    Staff
    Customer Support

    Hi there,

    Can you try in_category()?

    You can take a look at the WordPress conditional tag here:
    https://codex.wordpress.org/Conditional_Tags

    #932457
    Andy

    Yeh, I had tried both of the below using in_category but it still shows the excerpt on posts from the sub-category (id=5).

    add_filter( 'excerpt_length','tu_custom_excerpt_length', 1000 );
    function tu_custom_excerpt_length( $length ) 
    {
        // Return the normal length if we're not in this category
        if ( ! in_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_length','tu_custom_excerpt_length', 1000 );
    function tu_custom_excerpt_length( $length ) 
    {
        // Return the normal length if we're not in this category
        if ( ! in_category( array( 8,5 ) )) 
            return $length;
    
        global $wp_query; // assuming you are using the main query
        if ( 0 === $wp_query->current_post) {
            return $length;
        } else {
            return 0;
        }
    }
    #932595
    Leo
    Staff
    Customer Support

    Just checking, are you using custom excerpt? If not then the there is another filter you can use.

    Let me know 🙂

    #932601
    Andy

    I’m using the excerpt options in the Customizer > Layout > Blog > Excerpt Word count = 50

    #932622
    Leo
    Staff
    Customer Support

    Hmm can you try the conditional tag:
    is_category( 5 ) || cat_is_ancestor_of( 5, get_query_var( 'cat' ) )
    as suggested here?
    https://codex.wordpress.org/Conditional_Tags#A_Category_Page

    #932627
    Andy

    I’d actually also tried various versions of the below with no luck:

    add_filter( 'excerpt_length','tu_custom_excerpt_length', 1000 );
    function tu_custom_excerpt_length( $length ) 
    {
        // Return the normal length if we're not in this category
        if ( ! is_category( 8 ) || cat_is_ancestor_of( 8, get_query_var( 'advice' ) )) 
            return $length;
    
        global $wp_query; // assuming you are using the main query
        if ( 0 === $wp_query->current_post) {
            return $length;
        } else {
            return 0;
        }
    }

    Was hoping Tom could take a look.

    #932750
    Tom
    Lead Developer
    Lead Developer

    I wonder if this function would be better: https://codex.wordpress.org/Function_Reference/get_ancestors

    Your check would be:

    $ancestor_categories = get_ancestors( get_query_var( 'advice' ), 'category' );
    
    if ( ! in_array( 8, $ancestor_categories ) ) {
    
    }
    #932773
    Andy

    Sorry Tom, so what would the full filter look like?.

    #933239
    Tom
    Lead Developer
    Lead Developer

    Something like this maybe?:

    add_filter( 'excerpt_length','tu_custom_excerpt_length', 1000 );
    function tu_custom_excerpt_length( $length ) {
        $ancestor_categories = get_ancestors( get_query_var( 'advice' ), 'category' );
    
        // Return the normal length if we're not in this category
        if ( ! is_category( 8 ) && ! in_array( 8, $ancestor_categories ) ) 
            return $length;
    
        global $wp_query; // assuming you are using the main query
        if ( 0 === $wp_query->current_post) {
            return $length;
        } else {
            return 0;
        }
    }
    #933916
    Andy

    That looked promising but still not working :s

    #933947
    Tom
    Lead Developer
    Lead Developer

    Can you try the adjusted function above?

    #933951
    Andy

    Sorry, still no change unfortunately.

    #934451
    Tom
    Lead Developer
    Lead Developer

    Can you link me to a page where that condition should take effect?

    #934507
    Andy

    Hi Tom,
    I’ve edited my thread with a temporary public URL, it will only be live for 8hrs.

    Thanks.

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