• Resolved dominoeffect

    (@dominoeffect)


    Hi all,

    On my new site I’m using the following to pull up a list of recent posts & their excerpts on the sidebar:

    <?php query_posts('showposts=10');	?>
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<li><strong><a href="<?php the_permalink() ?>"><?php the_title() ?> </a></strong></li>
    
    <?php the_excerpt() ?>
    
    	<?php endwhile; endif; ?>

    Does anyone know of a way to shorten the length of the_excerpt?

    I’m looking for a way for customise the excerpt length automatically, to something more brief, say, ten words.

    I’m aware of the custom field, but adding an excerpt to that extra field (for each post) would be very time-consuming since I post at least once a day – I would have to count out the correct length of excerpt, and then paste it in, every time I post.

    Not ideal for a fast-moving blog!

    Thanks in advance, MS

Viewing 13 replies - 1 through 13 (of 13 total)
  • I’m sure there must be something in the Plugin Directory that will do what you want although I haven’t looked.

    Another method: insert the following in your theme’s function.php file,

    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'custom_trim_excerpt');
    
    function custom_trim_excerpt($text) { // Fakes an excerpt if needed
    global $post;
    if ( '' == $text ) {
    $text = get_the_content('');
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $text = strip_tags($text);
    $excerpt_length = x;
    $words = explode(' ', $text, $excerpt_length + 1);
    if (count($words) > $excerpt_length) {
    array_pop($words);
    array_push($words, '...');
    $text = implode(' ', $words);
    }
    }
    return $text;
    }

    Adjust the line $excerpt_length = x; to whatever you want.

    Yet another method (and one I don’t recommend) is to change the core file used to set excerpts. It is located at wp-includes/formatting.php Look for the line excerpt_length

    Thread Starter dominoeffect

    (@dominoeffect)

    Hi LenK,

    Thank you, the first fix worked a charm – I really appreciate the help.

    There is also this plugin: advanced-excerpt, which does roughly the same thing (plus more), but the board concensus is that it is quite buggy. And with your method that is one less plugin to track.

    Thanks again,

    LenK, your approach works nicely, but it appears there’s an even simpler way in WP 2.7. They’ve added an apply_filters call to the wp_trim_excerpt function:

    $excerpt_length = apply_filters(‘excerpt_length’, 55);

    So now, all you need to add to your theme’s functions.php is:

    add_filter(‘excerpt_length’, ‘my_excerpt_length’);
    function my_excerpt_length($length) {
    return 20; // Or whatever you want the length to be.
    }

    This seems better because it keeps you more closely in sync with the core code.

    Hello guys,

    @bluegrassboy, awesome help there. ??

    I realize however, the code only applies to excerpt that was extracted automatically.

    In the case of self-inputed excerpt feature in the WP editor, the code doesn’t apply (seems to overwrite that function). Is there a way around it?

    Thanks a bunch!

    Any way of getting this to apply to the RSS feed that is generated?

    I’ve created a simple plug-in for the WordPress backend to make it easier to change the length of the excerpt – without having modify any code.

    https://www.ads-software.com/extend/plugins/excerpt-length/

    Because it makes use of new API hooks, it only works in WordPress 2.7 and above.

    As for changing the length of the excerpt in the RSS feed, try this:

    In the backend, go to: /wordpress/wp-admin/options.php
    Find an option called rss_excerpt_length – the default value is 50, change that to the number of words that you want to appear.

    If there’s a demand for it, I may apply the default excerpt length to the RSS feeds too.

    Alternatively, if you still want to add the code to your theme’s functions.php, you can get it down to a one-liner:
    add_filter('excerpt_length', create_function('$a', 'return 20;'));

    I am working on applying what BluegrassBoy posted, that’s also shown on the WordPress Codex, but I can’t seem make it work.

    Can anybody explain how to use it further? Would I be just adding the add_filter on functions.php or I should do something else?

    Thanks

    Same issues as Simon here… I’m running 2.8.4, and none of the above solutions seem to have any effect in my functions file… Probably I’m missing something obvious (a brain) but nonetheless, help appreciated…

    I also tried the same solution that is shown in the codex:
    add_filter(‘excerpt_length’, ‘my_excerpt_length’);
    function my_excerpt_length($length) {
    return 250; // Or whatever you want the length to be.
    }

    And it does not appear to work or make any difference on WP 2.8.4 … the excerpt remains at 55!

    Works for me – did you guys forget to enclose the code in php tags in the functions file?

    eg:

    <?php
    add_filter('excerpt_length', 'my_excerpt_length');
    function my_excerpt_length($length) {
    return 10; // Or whatever you want the length to be.
    }?>

    i’ve been chasing down this bug . in my case its the eventcalendar3 plugin that is removing the default filter wp_trim_excerpt and overriding it with its own filter ( which is hardwired to 55 words ) . can i suggest that you try disabling that plugin if you have it or search all your plugins for ones that add_filters to excerpt hooks ?

    here is my fix ( to the event calendar plugin ) . i’ll point the developer to this post too .

    whale:wp-content rossetti$ grep -rni excerpt_length  wp-content/plugins/
    wp-content/plugins/event-calendar/eventcalendar3.php:558:    $excerpt_length=55;
    //    $excerpt_length=55;
    	$excerpt_length = apply_filters('excerpt_length', 55);
    gravityenemy

    (@gravityenemy)

    I got the plugin to work … but it only seems to work for the general site feed, what about the feed for the pages, they still seem to be limited to a certain length. Any ideas?

    yakitos

    (@yakitos)

    Why is this post marked as resolved? I am using 2.8.6 and the following code on my functions.php and it is not working.

    add_filter(‘excerpt_length’, ‘new_excerpt_length’);
    function new_excerpt_length($length) {
    return 10;
    }

    Which table can I check in order to see if the filter is actually there?

    Thanks!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Changing the default length of the_excerpt’ is closed to new replies.