• Hello

    I have a home page that takes excerpts from pages for certain parts and it also pulls in the latest news post in another part of the page.

    I want to have the three widgets with page excerpts having a certain length e.g. 41 words

    and the latest news post excerpt to have a different amount like 200 words.

    I have tried the following:

    function excerpt_length_category( $length ) {
    if (in_category( 'News' )) {
    return 200;
    } else {
    return 41;
    }
    }
    add_filter( 'excerpt_length', 'excerpt_length_category' );

    (added it to my functions.php file but it hasn’t made any difference and every excerpt is at 41. I’ve cleared caches etc.

    Any ideas?

    I’m using a child theme based on Spacious WordPress theme.

    Thanks in advance for any ideas

    Vicky

Viewing 11 replies - 1 through 11 (of 11 total)
  • As it says in the description,

    Prior to v2.7, this function could only be used in the WordPress Loop. As of 2.7, the function can be used anywhere if it is provided a post ID or post object.

    you need to give it the post to check.

    Moderator bcworkz

    (@bcworkz)

    $maybe_post = get_queried_object(); // query not always for a post
    if ( $maybe_post instanceof WP_Post && in_category( 'News', $maybe_post )) {
    Thread Starter Kanger

    (@kanger)

    Thank you both

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Have you tried changing the priority?

    
    add_filter( 'excerpt_length', 'excerpt_length_category', 100 );
    
    Thread Starter Kanger

    (@kanger)

    By changing priority do you mean:

    add_filter (‘excerpt_length_category’, ‘excerpt_length’ )

    ? Is the 100 word count for excerpt category?

    Thank you

    Thread Starter Kanger

    (@kanger)

    Thanks bcworkz

    would I need to add return 200 or something? or a filter too?

    something like this?

    $maybe_post = get_queried_object(); // query not always for a post
    if ( $maybe_post instanceof WP_Post && in_category( 'News', $maybe_post )) {
    	return 200;
    }
    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    So the priority is an argument passed to the add_filter function. Think of it as a link on a chain. You can change their priorities by removing and re-adding them at an earlier time or later time of execution.

    A plugin could be using

    
    add_filter( 'excerpt_length', 'excerpt_length_category' );
    

    and the theme could be using

    
    add_filter( 'excerpt_length', 'excerpt_length_category', 100 );
    

    The theme’s function will execute last because it has a higher priority. By default ( if you don’t specify ) the priority for all add_action and add_filter functions is 10. You can read about those arguments on the developer pages:

    https://developer.wordpress.org/reference/functions/add_action/
    https://developer.wordpress.org/reference/functions/add_filter/

    Does that make sense?

    Thread Starter Kanger

    (@kanger)

    I think so, kind of like z-index in css…

    thanks

    Moderator bcworkz

    (@bcworkz)

    On further thought, getting the post from the queried object is unreliable for some excerpts. The following should work, but it’s untested.

    function excerpt_length_category( $length ) {
        if (in_category( 'News', get_the_ID())) {
            return 200;
        } else {
            return 41;
        }
    }
    add_filter( 'excerpt_length', 'excerpt_length_category', 100 );
    Thread Starter Kanger

    (@kanger)

    Wow, you are a genius! It worked! thank you, put it into functions.php file and it’s all doing what I wanted, great! Thank you.

    Moderator bcworkz

    (@bcworkz)

    You’re welcome! Fully grasping an issue is my shortcoming. It often takes me few tries, but I usually get there 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Different lengths for Excerpt in different locations’ is closed to new replies.