Home  //  Blog  //  WordPress Functions  //  Variable Post Excerpt Lengths Based on Title Character Count

Variable Post Excerpt Lengths Based on Title Character Count

Posted on August 21, 2010 in WordPress Functions

If you are looking to create a post landing page layout that flows across multiple columns while maintaining a consistent height to each post coming through from the loop, then the following piece of code will do just that. It will allow you to set the number of characters output on the excerpt based on the title character count.

<?php $excerpt = strip_tags(get_the_excerpt());
// Limit $summary to how many characters?
 
$title_len = strlen($post->post_title); //get length of title
 
if($title_len <= 30){
	$limit=270; //calc space remaining for excerpt
}
else if($title_len <= 60){
	$limit=230; //calc space remaining for excerpt
}else if($title_len <= 75){
	$limit=180;
}
 
$summary = substr($excerpt, 0, strrpos(substr($excerpt, 0, $limit), ' ')) . '...';
echo '
 
'.$summary,'
 
';  ?>
<div class="da-more-link"><a href="<?php the_permalink(); ?>" title="Read Full Post: <?php the_title(); ?>">Read Full Post &raquo;</a></div>

The way this works is rather simple. The $title_len represents the length of the post title, while the $limit represents the excerpt length limit. In the first if statement, the title_len is less than 30 characters, then the excerpt will output 270 characters.

The next series of else if statement function in the same way, reducing the size of the excerpt as the title character counts increases. This example only displays 3 possible sizes, but add more else if’s if required.

The second portion of the code will create a summary by stripping the post and apply the conditional statement values, which will then be output onto the page wrapped in p tags. The first line of the summary also end with three periods, this can be changed to whatever you wish to output to end the excerpt text.

The read more link is the last line of the code, simply change the READ MORE LINK portion of the code to set the text for viewing the full post.

Let's Get Social

Use these icons to share this post with friends and colleagues on various social media avenues.

Got Something to Say?

Get involved in the discussion and leave a comment by filling out the form below. Don't have an avatar associated with your comments? Sign-up for a Gravatar today!

NugGrouggEm

Comment made on January 21, 2012 at 12:59 am

buy aygestin online no prescription

[Reply]

Emirlprarsoli

Comment made on January 24, 2012 at 6:34 pm

buy amitriptyline tablets uk

[Reply]

Submit Your Comment

Please leave a comment by filling out the form below. Comment must be approved before it becomes visible.

Your Comment

Note: You may use these HTML tags and attributes within your comment: <a href="" title="">, <em>, <strong>. To start a new paragraph, make sure you have a full line space between them using two returns.

© Troy Chaplin 2010  //  Site built using a Macbook Pro, Coda and Photoshop and is proudly powered by WordPress