Contact

Trim and Limit WordPress Content by character without plugin

by Rofikul Shahin

A very simple way to trim titles or content to fit into a design.

<?php $trimmed_content = wp_trim_words( get_the_content(), 20, "" ); ?>
<?php echo $trimmed_content; ?>
<?php $trimmed_title = wp_trim_words( get_the_title(), 20, "" ); ?>
<?php echo $trimmed_title; ?>

This will limit the content to a maximum of 20 words. the wp_trim_words() comes with three parameters, $text, $num_words and $more. $text is the source content, be it the title or content, $num deals with number or words and $more allows to add custom “More” indicator whenever a content is not under 20 words.

 

Simple!