Ready to get started?Download WordPress


[resolved] Changing length of the_excerpt without changing it on the whole wordpress (14 posts)

  1. buvesz
    Posted 4 years ago #


    I'm using the popular Category Post Widget to show the latest posts from a category on a sidebar of my website.

    The plugin uses the_excerpt to pull the excerpt and show it in the sidebar widget plugin.

    Basically, I'd like to change the length of the_excerpt that is shown, just in that plugin in the sidebar.


    On the above topic, some solutions to do this were presented but that would require to edit function.php and I don't want to do that because I don't want to affect all the instances of the_excerpt on my website, but just in the plugin.

    The ideal would be if there were a parameter to directly tell the_excerpt how many words to pull. Unfortunately, such a parameter don't exist, I think.

    So, is there a solution ?

    Maybe something else than the_excerpt to pull the excerpt with the possibility of choosing the number a words via some parameter. Or maybe a parameter for the_excerpt I don't know of ?

    Or another plugin that lets me do that? All of this seems pretty basic but after days I don't seem to have found a solution everywhere I looked. Any help would be highly appreciated. Thanks

  2. gerbilk
    Posted 4 years ago #

    can;t remember where i found this code. add the following to functions.php

    function excerpt($num) {
    $limit = $num+1;
    $excerpt = explode(' ', get_the_excerpt(), $limit);
    $excerpt = implode(" ",$excerpt)."...";
    echo $excerpt;
    function content($num) {
    $theContent = get_the_content();
    $output = preg_replace('/<img[^>]+./','', $theContent);
    $limit = $num+1;
    $content = explode(' ', $output, $limit);
    $content = implode(" ",$content)."...";
    echo $content;
    function title($num) {
    $limit = $num+1;
    $title = explode(' ', get_the_title(), $limit);
    $title = implode(" ",$title)."...";
    echo $title;


    <?php excerpt('25'); ?>
    you can specify the length each time you use it

  3. buvesz
    Posted 4 years ago #


    This would be perfect if it worked. But one thing I've never played with in WordPress if the function.php file. And I tried to add that code to the function.php file but it doesn't work. Should I put it somewhere specific in function.php or should it not matter ?

  4. gerbilk
    Posted 4 years ago #

    put it at the bottom but before ?> also try without the function title block of code, it doesn't work, sorry i didn't mean to post that section. Empty lines can sometimes break the functions.php as well.

  5. buvesz
    Posted 4 years ago #

    Ok I'll try that. But what do you mean by function title block of code ?

    Sorry to ask. I'm not too familiar with function.php as I said before.

  6. gerbilk
    Posted 4 years ago #

    the third section of code that begins function title($num)

  7. buvesz
    Posted 4 years ago #

    It works!

    Very nice work around! And very useful

    Thanks a lot

  8. eivindvr
    Posted 4 years ago #

    I tried it to and it work fine, but not in my "avatrs in recent comments" widget:-(

    I also tried this: http://jarretcade.net/changing-the-default-wordpress-excerpt-length

    I belive that is the best way to change the default exerpt.


  9. eivindvr
    Posted 4 years ago #

    you could also try this:

    `function my_excerpt_length($text){
    return 10;

    in the functions file

  10. Ufuk Erdogmus
    Posted 4 years ago #

    Thank you for the code you shared gerbilk :)

    I have a small correction to make about the title one:
    should obviously be

    just making sure for the newbies.

    And on each of them:
    $title = "<h2>".implode(" ",$title)."...</h2>";
    or <p> ...</p> for the excerpt etc.
    makes it CSS-friendly ;)

  11. gerbilk
    Posted 4 years ago #

    doh! thanks!

  12. Anydog
    Posted 4 years ago #

    Yep ! Very usefull ! Thanks gerbilk, you're the man ! ;)

  13. Anydog
    Posted 4 years ago #

    Hmm, since you are so generous with usefull code, is there a way to limit the excerpt, content or title by number of letters, not words ? :)

  14. sagive
    Posted 3 years ago #

    wow - a gr8 solution :)
    10x man.. gona use it for multiple themes!

Topic Closed

This topic has been closed to new replies.

About this Topic