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?

    $id = 9;
    $post = get_page($id);
    $content = apply_filters('the_content', $post->post_content);
    echo $content;
  • Words or characters?
    – fuxia
    Commented Nov 21, 2012 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
    Commented Nov 21, 2012 at 11:04

4 Answers 4


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.


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.

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;
  • Hiya - would that go in the functions file?
    – V Neal
    Commented Nov 21, 2012 at 11:48
  • Added an example to the answer above Commented Nov 21, 2012 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
    Commented Nov 21, 2012 at 12:22
  • that line just tells wordpress that a 'page' can also have an 'excerpt'. Commented Nov 21, 2012 at 13:02
  • You assume that new excerpt will be always shorter than previous one. Is it correct assumption? Commented Jun 23, 2013 at 8:25

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 );

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
  • 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); } Commented Jun 24, 2013 at 8:12

dont over complicate this

if you use [get_the_content][1] in the loop you can use [wp_trim_words][2].

$id = 9;
$post = get_page($id);
$content = $post->post_content ; 
echo wp_trim_words($content, 55, '<a href=""> Read More Link</a>');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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