Support » Fixing WordPress » controlling the excerpt / the content

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

    but of course its applying to all excerpts.

    So I tried to apply the filter only once:

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


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


    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?


Viewing 3 replies - 1 through 3 (of 3 total)
  • 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
    $my_global_excerpt_limit = 0;  // Clear the filter

    Fantastic vtxyzzy, this is working so beautyfully!

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


Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘controlling the excerpt / the content’ is closed to new replies.