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've the below php to display the content of the post id#9 on my 'home page' of my site, so that whenever the client updates the page, it's reflected on the homepage as well - rather than having to make the change twice.

At the moment all the post content is being shown, I need to strip it down to about 100 words...

Any ideas?

<?php
    $id = 9;
    $post = get_page($id);
    $content = apply_filters('the_content', $post->post_content);
    echo $content;
?>
share|improve this question
    
Words or characters? –  toscho Nov 21 '12 at 11:02
    
Words if possible? I've googled it - but all the other solutions seem to be quite chunky. This seems the neatest way of doing it? –  V Neal Nov 21 '12 at 11:04
add comment

3 Answers

For the excerpt, you can use this function but for the content, it will create problems with the html, so you'll have to handle that yourself

function limit_words($phrase, $len) {
    $len = (int) $len;
    if (str_word_count($phrase) > $len) {
        $keys = array_keys(str_word_count($phrase, 2));
        $phrase = substr($phrase, 0, $keys[$len]);
    }
    return $phrase;
}

The above goes into functions.php. Then you can use it anywhere inside your template files or even your functions.php file. For eg. in a template file

$excerpt = get_the_excerpt();
$new_excerpt = limit_words($excerpt, 10);
// change 10 to the number of words
echo $new_excerpt;
share|improve this answer
    
Hiya - would that go in the functions file? –  V Neal Nov 21 '12 at 11:48
    
Added an example to the answer above –  Mridul Aggarwal Nov 21 '12 at 11:58
    
Ah i see.. although wouldn't this do a similar job? add_post_type_support( 'page', 'excerpt' ); And yes, the HTML is stripped in both versions - not ideal for what I wanted really. –  V Neal Nov 21 '12 at 12:22
    
that line just tells wordpress that a 'page' can also have an 'excerpt'. –  Mridul Aggarwal Nov 21 '12 at 13:02
    
You assume that new excerpt will be always shorter than previous one. Is it correct assumption? –  Krzysiek Dróżdż Jun 23 '13 at 8:25
show 1 more comment

WordPress has built in functions to do this. The only problem is that it assumes, that all your excerpts should have the same max-length.

If this assumption is OK, all you have to do is to define this maximal value.

You can do this with this filter (put it into functions.php file):

function my_excerpt_length( $length ) {
    return 40;  // put new excerpt length in here
}
add_filter( 'excerpt_length', 'my_excerpt_length' );

Then in your template file you just have to do this:

setup_postdata( $post );
the_excerpt();

If you need to use different excerpt lengths in different places on your site, then you can use something like this (add this function to functions.php):

function my_trim_excerpt($text = '', $excerpt_length =0) {
    if ( !$excerpt_length )
        $excerpt_length = apply_filters('excerpt_length', 55);

    $raw_excerpt = $text;
    $text = strip_shortcodes( $text );

    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]&gt;', $text);
    $excerpt_length = apply_filters('excerpt_length', 55);
    $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );

    return apply_filters('get_the_excerpt', apply_filters('wp_trim_excerpt', $text, $raw_excerpt));
}

Then use it like this:

echo apply_filters('the_excerpt', my_trim_excerpt($post->post_content, 40)); // 40 is the excerpt length
share|improve this answer
1  
Haven't tested but if all you want is to add the length parameter to wp_trim_excerpt, then this would be more dry function my_trim_excerpt($text = '', $length = 0) { if($length && is_numeric($length) { $function = create_function('', 'return '.intval($length).';'); add_filter('excerpt_length', $function, PHP_INT_MAX);/* running last */ $text = wp_trim_excerpt($text); remove_filter('excerpt_length', $function); return $text; } return wp_trim_excerpt($text); } –  Mridul Aggarwal Jun 24 '13 at 8:12
add comment

WordPress has inbuilt function force_balance_tags which I belive is not explored much but it can be useful in such scenario.

get the content and truncate it to any arbitrary length

$content = get_the_content();

$length = 1000;

if(strlen($content) > $length) {
    $content = substr($content, 0, $length);
}

Now apply force_balance_tags function to balance any html tags which were started in content but did not end due to string length limit.

echo force_balance_tags($content);

Hope it helps.

EDIT:

From the WordPress Documentation page

This function is used in the short post excerpt list, to prevent unmatched elements. 
For example, it makes <div><b>This is an excerpt. <!--more--> and this is more text... </b></div> not break, 
when the html after the more tag is cut off.
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.