Get a limited, text-only excerpt from a WordPress post? Get a limited, text-only excerpt from a WordPress post? wordpress wordpress

Get a limited, text-only excerpt from a WordPress post?


Avoid using substr(). Why?

substr() truncates based on character count, NOT whole words, and the last word would most likely be truncated. It could also truncate the end HTML tag(s) and return malformed HTML, screwing up the rest of your layout.

Don't re-invent the wheel!

WordPress 3.3 onwards has a new core function called wp_trim_words()

Parameter breakdown:

wp_trim_words($text, $num_words, $more);$text    (string) (required) Text to trim    Default: None$num_words    (integer) (optional) Number of words    Default: 55$more    (string) (optional) What to append if $text needs to be trimmed.    Default: '…'

Example usages:

<?php echo wp_trim_words(get_the_excerpt(), 30, '...'); ?><?php echo wp_trim_words(get_the_content(), 50, '... read more >'); ?>


You could try using something like this to grab the first 20 words of the post if there is no excerpt available.

$content = get_the_content();echo substr($content, 0, 20);


try this :

Post contains images :

$content = get_the_content();$content = apply_filters('the_content', $content);$content = str_replace(']]>',']]>', $content);echo substr(strip_tags($content),0,100); 

and without images:

 $content = get_the_content(); echo substr($content, 0, 25);