Support » Themes and Templates » Trim explicit excerpt

  • I am building a page that will include a single main post area that will show a featured picture and excerpt from a post and below that will show older posts with just their excerpts. I intend to devote enough space to the new/main post to allow it to be say 200 words long. However for the old posts I only want to devote enough space for say 100 words. Of course this will impact the way that I write my excerpts so that the first 100 words are a sufficient “hook”.
    I have seen some code that allows you to change the length of an excerpt generated from the content of the post but nobody seems to tackle the_excerpt. How I can I write some code that will retrieve the explicit excerpt (the_excerpt) and return only the first 100 words? Can I do it on a sentence level?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Michael


    Forum Moderator

    have a look at this article: automatically-shorten-manual-excerpt

    if you change this line:
    $text = strip_tags($text);
    you can include html tags that you want to preserve for the excerpt:

    I just tried that approach and it seems to do a great job of trimming the length of a declared excerpt. However, it trimmed all of my excerpts and I’m not sure how to set it up so that I can declare within a style sheet if an excerpt should be trimmed or not.



    Forum Moderator

    if you are prepared to use your own function to make the excerpt, you could use:

    function custom_trim_excerpt($length) {
    global $post;
    $explicit_excerpt = $post->post_excerpt;
      if ( '' == $explicit_excerpt ) {
        $text = get_the_content('');
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
      else {
        $text = apply_filters('the_content', $explicit_excerpt);
    $text = strip_shortcodes( $text ); // optional
    $text = strip_tags($text);
    $excerpt_length = $length;
    $words = explode(' ', $text, $excerpt_length + 1);
      if (count($words)> $excerpt_length) {
        array_push($words, '[…]');
        $text = implode(' ', $words);
        $text = apply_filters('the_excerpt',$text);
    return $text;

    this needs to be saved to functions.php of the theme.

    the function uses the explicit excerpt if filled in, or makes an excerpt from the content; and trims both to the max length as specified in the parameter.

    in the template files, where you want to show the excerpt, you need to add:
    <?php echo custom_trim_excerpt(100); ?>

    the parameter is the cut-off length of the excerpt.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Trim explicit excerpt’ is closed to new replies.