WordPress 3.3 brought a handy little function with it in wp_trim_words() . An example use case is a height limited box like the one pictured, where you want to make sure that the title and excerpt don’t cause the text to overflow the box.

Using wp_trim_words() allows us to limit the title and excerpt, and anything else really, to a particular number of words. How practical a name. You can define the number of words to limit them to, and also define what you’d like the “more” text to be. My example just uses ellipses.

Below is a gist to show it in use. Everything but the use of wp_trim_words() is totally arbitrary, but there for fun.

<div class="info-box"> <?php $krogsquery = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => 3 )); ?> <h3 class="info-headline">City News</h3> <?php while($krogsquery->have_posts()) : $krogsquery->the_post(); ?> <div <?php post_class(); ?> id="post-<?php the_ID(); ?>"> <?php $trimtitle = get_the_title(); $shorttitle = wp_trim_words( $trimtitle, $num_words = 4, $more = '… ' ); echo '<h6 class="info-title">' . '<a href="' . get_permalink() . '">' . $shorttitle . '</a></h6>'; $trimexcerpt = get_the_excerpt(); $shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… ' ); echo '<a href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>'; ?> </div><!-- post class --> <?php endwhile; ?> <?php wp_reset_postdata(); // reset the query ?> </div><!-- .info-box -->

Now quit reading and go make some stuff.