I needed to change the size of the post summary / excerpt depending on the length of the title for a recent project. The goal was to fit both the post title and excerpt within a fixed-size container, while being able to adjust for changing title and excerpt sizes from post to post. This is how I did it.
I was working on a custom Wordpress blog design for a client and, unlike most blog designs, we needed to fit the post summary (or excerpt) into a fixed area. Traditional blog designs won’t break if the post excerpt exceeds a certain size, but in this case, we had to keep both the title, and the excerpt height no greater than the image as shown below.
We knew the image size, but the title and excerpt sizes were the variables here. We didn’t want to cut off the title, so we had to figure out a way to check how much space the title occupied, then figure out how much space was remaining for the excerpt. If the excerpt was larger than the space remaining, we needed to trim the excerpt toward the end and add [...] to indicate the excerpt was truncated.
The following photos are of the homepage layout…each post was shown on the homepage as one of these wide, image, title, and excerpt combinations.
The first photo below shows how we wanted the excerpts to be displayed. The second shows what happens if the excerpt isn’t truncated based on the title size…not good.


This will require you to put your thinking cap on, so dig it out from under that pile of clothes, beer cans, or whatever it is you leave all around your house, and set it top of that little idea factory you call your head.
The Code I Used
Here’s the code I used to get it done…you’re welcome to copy this code, but be sure to read the rest of this post as you’ll have to change a few things depending on the font family and font sizes used on your blog.
<?php
// Dynamically resize excerpt according to title length
$rem_len = ""; //clear variable
$title_len = strlen($post->post_title); //get length of title
if($title_len <= 35){
$rem_len=188; //calc space remaining for excerpt
}elseif($title_len <= 70){
$rem_len=146;
}elseif($title_len <= 105){
$rem_len=104;
}elseif($title_len <= 140){
$rem_len=62;
}
$trunc_ex = substr($post->post_excerpt, 0, $rem_len); //truncate excerpt to fit remaining space
if(strlen($trunc_ex) < strlen($post->post_excerpt)) $trunc_ex = $trunc_ex . " [...]";
echo "<p>" . $trunc_ex . "</p>"; //display excerpt
?>
Now the explanation
First, I took the font used for post titles on the homepage and calculated (roughly) how many characters it took to fill out a full line within the space allocated. That number turned out to be about 35 characters per line.

Then I did the same for the post excerpt font. Turns out that the post excerpt font takes up 42 characters per line.

Then I figured, since every 35 characters in the title takes up one line, and 42 characters of the excerpt occupy a full line, then for every 35 characters in the post title, that was 42 more characters I’d have to truncate off the excerpt text.
You also have to consider the fact that the post title is a block level element. In other words, even if the post title only takes up half a line, the post excerpt won’t begin until the following line.
How to Edit the Code for Your Blog
So, to make it work for you, you need to figure out the maximum number of excerpt characters that will fit inside your div element along with 1 line of post title. For me that was 5.
Then edit the code I gave you above as follows:
Replace the number 35 in the following line with the number of characters per line for your title:
if($title_len <= 35){
Then skip a line and replace the number 70 in the following line with 2 x the number of characters per line for your title:
}elseif($title_len <= 70){
Then skip another line and replace the number 105 in the following line with 3 x the number of characters per line for your title (are you seeing a pattern here?):
}elseif($title_len <= 105){
Then skip another line and replace the number 140 in the following line with 4 x the number of characters per line for your title:
}elseif($title_len <= 140){
Now, you may want to keep going, I repeated this 4 times because I figured I could fit one line of excerpt in with 4 lines of title at the most.
Now you need to go back and remember the total number of excerpt characters you figured would fit inside that div element. Got that number in front of you? Good. Now you’ll need the number of characters per line for your excerpt font. Now subtract the number of characters per line from the total excerpt characters that could fit in the div element. Whatever that number comes to, put it in place of 188 in the following line of code from above:
$rem_len=188; //calc space remaining for excerpt
Now take the number of excerpt characters per line x 2 and subtract that from the total excerpt characters that would fit inside the div. Now put it in place of 146 in the following line of code from above:
$rem_len=146;
Now take the number of excerpt characters per line x 3 and subtract that from the total excerpt characters that would fit inside the div. Now put it in place of 104 in the following line of code from above:
$rem_len=104;
And finally, take the number of excerpt characters per line x 4 and subtract that from the total excerpt characters that would fit inside the div. Now put it in place of the 62 in the following line of code from above:
$rem_len=62;
That’s it. Now your excerpt should automatically resize itself depending on the length of the post title.


i am taken with the concept however the php is over my head so i have to ask; where in the loop is this to be placed?
i used it under
$recent = new wp_Query(“cat=27&showposts=1″); while($recent->have_posts()) : $recent->the_post()
(with the requisite “”)
and got error messages…
any advice?
@madbadcat: None of that php actually displays anything except for that last line with the “echo”…so, barring a cleaner solution, you can put it wherever you want the excerpt to show up at…i.e. Right after the title.
How do you excerpt the image? My wordpress doesnt seems to excerpt the image at all. Unless I write my excerpt myself under the text editor.
In the example above, I used a custom field to set the image that would appear on the home page, so it’s not a part of the excerpt itself.
Wonderful post, very informative.
I’m attempting to use the substr function to remove a number of characters from the post title. I’ve tried to use the “get_the_title(ID)” function with no luck yet. Would you have any suggestions? Here’s where I’m working on it: http://dev.joshellington.com/hype/features/
@Josh: Have you tried $post->post_title to retrieve the post title (I think that works within the loop only)
Thanks for posting this, it helps me a lot.
I want to write another case for if the post author did not include an excerpt at all. If not, then I want to make the excerpt for them with this same code. Kind of as a fail safe. Is that possible?
If there's no excerpt, you'll need to get a snippet of the post content. Off the top of my head, the best way to do that would probably be:
1. Test if post excerpt is longer than ""
2. If there's no post excerpt, use the $post->post_content instead of $post->post_excerpt in the above code
That should do it, but I think $post->post_content will return html tags and all, so you may need to strip out any formatting.
My $post variable quick-reference may help a bit as well.
http://www.rlmseo.com/blog/wordpress-post-variabl...
great concept!
I do have a problem trying to use …
What's the problem?
oo…I must have pressed enter too early
For cert…
geez…again somehow. k, I won't hit Enter Certain posts don't show an excerpt at all. These posts are those that have some tag near the beginning such as img or a. My guess is strlen and strsub fumble when they hit these tags. Any ideas?
Have you tried printing the excerpt for those posts before using string functions to make sure wordpress is actually returning an excerpt in the first place?
As an aside, you should be able to hit enter on the comments text area without it submitting. This is a new comment system, so if anyone else is having that problem, let me know so I can look into it.
yeah I don’t believe I’m using anything that would not return an excerpt.
Here’s my loop:
<?php query_posts(‘category_name=home_post9′); ?>
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<div class=”posts”>
<div class=”lin”></div>
<div class=”postim”>
<img src=”<?php $mykey_values = get_post_custom_values(‘image’); foreach ( $mykey_values as $key => $value ) {echo $value . “”;}?>”>
</div>
<h1><?php the_title(); ?></h1>
<!— INSERT YOUR CODE HERE –>
</div>
<?php endwhile; ?>
<?php endif; ?>
I just notice I gave a solution in the case the author doesn't write an excerpt in response to packfan77 above. Did you try that?