Is there an easy method of adding the last few words of the posts, added to the trimmed excerpt that is automatically generated for post displayed on category pages?

Currently it will only show the first given number of words, and I would like it to have the first few words and the last few words, something like following

1 2 3 4 5 6 [...] 997 998 999 1000

2 Answers 2


You can achieve this by using the_excerpt filter. You can read further on the codex. Just paste this code in functions.php and set the value of $i = no of words from the end you want.

function my_excerpt(){
    global $post;                         
    $content = get_the_content(); //gets the whole content
    $content =strip_tags($content) ; //strips html tags                  
    $content = explode(" ", $content); //stores each word in an array
    $size = count($content);        //counts the length of array                                
    $last="";                       //initialize an empty sting
    $i=4;                           //no of words from the last you want
        $last.= $content[$size-$i];   
        $last.=" ";
    return $excerpt."......".$last;

Now use the_excerpt(); to output the kind excerpt you want. All the best and let me know if it worked or not.

  • this is great however it also outputs the code and html tags and image tags as the end string and not only the actual text of the last words, I would like for it to display the last few words of the formatted content Jul 19, 2015 at 8:49
  • 1
    Use wp_strip_all_tags() or wp_filter_nohtml_kses() on the content returned by get_the_content() to get rid of the tags. @webestdesigns Jul 19, 2015 at 8:58
  • @ialocin Thanks I used $content = wp_strip_all_tags(get_the_content()); Jul 19, 2015 at 9:05

You can wordpress excerpt using maximam character OR word count and display like

Loriem ipsum[...]

so I have solution.

This code put in function.php

 *  The Excerpt for word counting in the_content or excerpt.
function getCharCut($text, $limit)
    return  $char_cut; 
    return esc_html($char_cut." [...]");    

You can use the excerpt or content count character OR word .

            $mycontent = get_the_excerpt();
            echo getCharCut($mycontent, 105);
  • Please do not tag answers Jul 19, 2015 at 5:46
  • Thanks but I would like to cut the middle, and to display both the beginning of the post content and the end of the post content. How would I be able to display the last few words as well as the first few words? not to just count the x number of words from the beginning to display. Jul 19, 2015 at 8:58
  • I am useing Tag because tag to search and display answer in strackoverflow. Like, wordpress is a Tag to search people and get Result . Jul 20, 2015 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.