WordPress.org

Ready to get started?Download WordPress

Forums

controlling the excerpt / the content (4 posts)

  1. supervision
    Member
    Posted 2 years ago #

    Hey guys,

    only on one point on the website I need a custom excerpt, only 20 words instead of the normal 40.

    Had a look at:
    http://codex.wordpress.org/Plugin_API/Filter_Reference/excerpt_length

    but of course its applying to all excerpts.

    So I tried to apply the filter only once:

    http://codex.wordpress.org/Function_Reference/apply_filters#Description

    but I dont get the syntax...tried something like:

    echo apply_filters('the_content', excerpt_length, custom_excerpt_length, 999);

    and its obviously wrong.

    And in the end I thought I could just simply shorten the text with simple php substr:

    echo substr(the_content(), 0, 20);

    and

    echo substr(the_excerpt(), 0, 20);

    and

    echo substr(wp_trim_excerpt(the_content()), 0, 20);

    But NONE of this is working, its like the substr command is completly IGNORED, so I am kind of stuck here, and really dont get why substr() isnt working at all.

    You have an idea?

    TIA,
    sv

  2. vtxyzzy
    Member
    Posted 2 years ago #

    Your substr is not working because the_excerpt() does not return the excerpt content, it sends it directly to the output.

    You can control when the custom function returns a special value using a function like this:

    function custom_excerpt_length( $length ) {
       global $my_global_excerpt_limit;
       if ( intval($my_global_excerpt_limit) )
    	return intval($my_global_excerpt_limit);
    
       return $limit;
    }
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

    Then, call the_excerpt like this:

    $my_global_excerpt_limit = 20;  // Set the filter
    the_excerpt();
    $my_global_excerpt_limit = 0;  // Clear the filter
  3. supervision
    Member
    Posted 2 years ago #

    Fantastic vtxyzzy, this is working so beautyfully!

    If its even possible to add a filter counting the characters instead of the words?

    cheers,
    sv

  4. vtxyzzy
    Member
    Posted 2 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic