Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a website in which I need to control the displayed excerpt length. Some of the posts might have manual excerpt so I can't use the excedrpt_length filter.

I can of course4 use somekind of substr but was looking for a more elegant solution (if such exists)

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

Take a look on my answer here: Best Collection of Code for your functions.php file

If I understood your question correctly, it does what you are looking for.

Place this in functions.php:

function excerpt($num) {
    $limit = $num+1;
    $excerpt = explode(' ', get_the_excerpt(), $limit);
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt)."... (<a href='" .get_permalink($post->ID) ." '>Read more</a>)";
    echo $excerpt;
}

Then, in your theme, use the code <?php excerpt('22'); ?> to limit the excerpt to 22 characters.

:)

share|improve this answer
1  
Your function should use a proper integer and not a string to pass numeric values or could just convert inside the function, eg. $num = (int) $num;. '1' != 1 .. :) –  t31os Jan 4 '11 at 15:46
add comment

Try this: You can control the amount of words the excert outputs with the filter "excerpt_length" below are a couple examples of how you can control the size based on different conditions

add_filter( 'excerpt_length', 'new_excerpt_length' );
function new_excerpt_length( $more ) {
    if(is_front_page()){
        if(has_post_thumbnail()){
            return 15;
        } else {
            return 45;
        }
    } else {
        return 100;
    }
}

EDIT: Crap, I just noticed that you said the filter approach was a no go. oh well this is for other people who get here via google and want this then.

share|improve this answer
add comment

@MartinAleksander Please correct me if I am wrong but your excerpt($num) functions returns the first $num words in excerpt and not the first $num characters. Thanks.

share|improve this answer
add comment

I'd say just look at how core does it: http://phpxref.ftwr.co.uk/wordpress/wp-includes/formatting.php.source.html#l1840

I took the liberty of putting the code here for ease of copying and pasting.

global $post;
if( empty($post->post_excerpt) ){
  $text = apply_filters( 'the_excerpt', get_the_excerpt() );
} else {
  $text = $post->post_excerpt;
  $text = strip_shortcodes( $text );
  $text = apply_filters('the_content', $text);
  $text = str_replace(']]>', ']]&gt;', $text);
  $text = strip_tags($text);
  $excerpt_length = apply_filters('excerpt_length', 55);
  $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
  $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
  if ( count($words) > $excerpt_length ) {
    array_pop($words);
    $text = implode(' ', $words);
    $text = $text . $excerpt_more;
  } else {
    $text = implode(' ', $words);
  }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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