1

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

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.

add_filter('the_excerpt','my_excerpt');
function my_excerpt(){
    global $post;                         
    $excerpt=get_the_excerpt();
    $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
    while($i>0){
        $last.= $content[$size-$i];   
        $last.=" ";
        $i--;  
    }  
    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.

3
  • 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 – webestdesigns Jul 19 '15 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 – Nicolai Jul 19 '15 at 8:58
  • @ialocin Thanks I used $content = wp_strip_all_tags(get_the_content()); – webestdesigns Jul 19 '15 at 9:05
1

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)
{
    $char_cut=substr(strip_tags(stripslashes($text)),0,$limit);
    if(strlen($text)<=$limit) 
    return  $char_cut; 
    else
    return esc_html($char_cut." [...]");    
} 

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

    <?php
        if(get_the_excerpt())
        {
            $mycontent = get_the_excerpt();
            echo getCharCut($mycontent, 105);
        }           
    ?>
3
  • Please do not tag answers – Pieter Goosen Jul 19 '15 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. – webestdesigns Jul 19 '15 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 . – Mahavar Hitesh Jul 20 '15 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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