Shorten Post Content for Display in Theme

7:11:00 PM | ,

Invoke Function

The first parameter in the limit_words function is the content you wish to extract the amount of words (second parameter) from.
1<?php echo limit_words(get_the_content(), '40'); ?>

Function Declaration

1<?php
2function limit_words($string$word_limit)
3{
4    $string = strip_shortcodes($string);
5    $string strip_tags($string);
6
7    $string = preg_replace("/&#?[a-z0-9]{2,8};/i","",$string);
8    $words explode(' '$string);
9    if (count($words) > $word_limit) {
10    return implode(' 'array_slice($words, 0, $word_limit)) . " ...";
11    else {
12    return implode(' 'array_slice($words, 0, $word_limit));
13    }
14}
15?>