Limit WordPress Post excerpt to specific physical size

My theme is a magazine style layout. On the front page, I have four columns that each show a preview of the most recent post from four different categories.

I would like for all of the excerpts to fill the allotted space and to display the <more...> link.

It is easy enough to set up a widget that posts a specific number of words, but because words come in many different sizes, I may have two excerpt that each contain 30 words, but one may take up five lines and the one next to it seven.

I can make a div to display the excerpt with the overflow set to hidden, but that hides the <more...> link at the end.

How can I make my post previews fill a specific space so that they each match each other?Desired outcome
LVL 26
David BruggeAsked:
Who is Participating?
 
Immanuel KumarPHP DeveloperCommented:
Hi,put this code in the template function.php

<?php
function string_limit_words($string, $word_limit)
{
  $words = explode(' ', $string, ($word_limit + 1));
  if(count($words) > $word_limit)
  array_pop($words);
  return implode(' ', $words);
}
?>

and call this in your display page.

<?php
  $excerpt = get_the_excerpt();
  echo string_limit_words($excerpt,25);
?>
Thank You
0
 
Jason C. LevineNo oneCommented:
Try switching to a character count solution:


<?php $little_excerpt = substr(the_excerpt(),0,X); ?>
0
 
David BruggeAuthor Commented:
Sorry for the delay. This one slipped past me.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.