How to create short text excerpts using wp_trim_words in WordPress

The WordPress wp_trim_words function is used to trim a given piece of text to a specified number of words. This can be useful when you want to limit the amount of content displayed on a page, such as in a blog post excerpt or a sidebar widget. It allows you to control the length of the text displayed, making it easier for users to scan and navigate through your content.

Parameters Accepted by wp_trim_words Function

The wp_trim_words function accepts the following parameters:

  • $text (string, required): Text to trim.
  • $num_words (int, optional, default value: 55): Number of words to trim the text to.
  • $more (string, optional, default value: null): What to append if the text needs to be trimmed. The default is ‘…’.

The function returns a string, which is the trimmed text.

Examples

How to use wp_trim_words to limit the number of words in a post

<?php
$content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
$trimmed_content = wp_trim_words( $content, 10, '...' );
echo $trimmed_content;
?>

This code snippet uses the wp_trim_words function to limit the number of words in the $content variable to 10, with an ellipsis (…) added at the end. The trimmed content is then echoed to the screen.

How to use wp_trim_words in a WordPress loop to limit the number of words in a post

<?php
if ( have_posts() ) {
 while ( have_posts() ) {
 the_post();
 $content = get_the_content();
 $trimmed_content = wp_trim_words( $content, 20, '...' );
 echo $trimmed_content;
 }
}
?>

This code snippet demonstrates using the wp_trim_words function within a WordPress loop to limit the number of words in each post’s content to 20, with an ellipsis (…) added at the end. The trimmed content is then echoed to the screen.

How to use wp_trim_words with a custom post type in WordPress

<?php
$args = array(
 'post_type' => 'my_custom_post_type',
 'posts_per_page' => 5
);
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) {
 while ( $custom_query->have_posts() ) {
 $custom_query->the_post();
 $content = get_the_content();
 $trimmed_content = wp_trim_words( $content, 15, '...' );
 echo $trimmed_content;
 }
 wp_reset_postdata();
}
?>

This code snippet showcases using the wp_trim_words function with a custom post type in WordPress. It uses a custom query to retrieve 5 posts of the custom post type ‘my_custom_post_type’ and then trims the content of each post to 15 words, adding an ellipsis (…) at the end before echoing the trimmed content to the screen.

Conclusion

In conclusion, the wp_trim_words function is a valuable tool for developers looking to limit the number of words displayed in a WordPress post or excerpt. By using this function, you can easily control the length of your content and improve the overall user experience on your website. Whether you’re building a custom theme or plugin, incorporating wp_trim_words can help you achieve a cleaner, more professional look for your site. With its simple syntax and powerful functionality, this function is a must-have for any WordPress developer’s toolkit.