WordPress.org

Ready to get started?Download WordPress

Forums

Excerpt, functions and filters (2 posts)

  1. arturocivit
    Member
    Posted 1 year ago #

    Hi everybody, I have a function that helps me to make the excerpt shorter, is this one:

    function custom_excerpt_length( $length ) {
    	return 20;
    }
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

    I have a spot within my site where the excerpt needs to be longer, let's say, 40 words instead 20, what should I do here? create another function like the above but with a different name? how to call the <?php the_excerpt(); ?> tag?

    I checked the codex but not so clear about what to do in order to achieve this, any idea would be great,

    Thanks!

    A.-

  2. vtxyzzy
    Member
    Posted 1 year ago #

    One possibility is to define a global variable that will contain a custom length and check for that in the function.

    function custom_excerpt_length( $length ) {
    	global $my_custom_excerpt_length;
    	if ( is_numeric($my_custom_excerpt_length) ) {
    		return $my_custom_excerpt_length;
    	} else {
    		return 20;
    	}
    }

    In the place you want a non-default length, code this:

    global $my_custom_excerpt_length;
    $my_custom_excerpt_length = 40;

    After calling the_excerpt(), set the value to zero so other excerpts will not be affected.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.