Support » Plugins » Changing length of the_excerpt without changing it on the whole wordpress

  • Resolved buvesz

    (@buvesz)


    Hi,

    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.

    http://wordpress.org/support/topic/231953

    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

Viewing 13 replies - 1 through 13 (of 13 total)
  • 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);
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt)."...";
    echo $excerpt;
    }
    
    function content($num) {
    $theContent = get_the_content();
    $output = preg_replace('/<img[^>]+./','', $theContent);
    $limit = $num+1;
    $content = explode(' ', $output, $limit);
    array_pop($content);
    $content = implode(" ",$content)."...";
    echo $content;
    }
    
    function title($num) {
    $limit = $num+1;
    $title = explode(' ', get_the_title(), $limit);
    array_pop($excerpt);
    $title = implode(" ",$title)."...";
    echo $title;
    }

    then

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

    Thread Starter buvesz

    (@buvesz)

    Hi.
    Thanks.

    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 ?

    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.

    Thread Starter buvesz

    (@buvesz)

    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.

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

    Thread Starter buvesz

    (@buvesz)

    It works!

    Very nice work around! And very useful

    Thanks a lot

    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.

    Eivind

    you could also try this:

    `function my_excerpt_length($text){
    return 10;

    in the functions file

    Thank you for the code you shared gerbilk 🙂

    I have a small correction to make about the title one:
    array_pop($excerpt);
    should obviously be
    array_pop($title);

    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 😉

    gerbilk

    (@gerbilk)

    doh! thanks!

    Yep ! Very usefull ! Thanks gerbilk, you’re the man ! 😉

    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 ? 🙂
    ThnkU

    wow – a gr8 solution 🙂
    10x man.. gona use it for multiple themes!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Changing length of the_excerpt without changing it on the whole wordpress’ is closed to new replies.