WordPress Code Snippets

A fresh mix of code snippets and tutorials

Custom title length

48

Quick tip for limiting the number of characters displayed in post titles. Limiting title length is useful in some design situations and is actually very easy to do.

To customize the length of your WordPress post titles, add the following code to functions.php:

function short_title($n) {
	$title = get_the_title($post->ID);
	$title = substr($title,0,$n);
	echo $title;
}

Then to use this code to display your shortened titles, include the following template tag anywhere in the WordPress loop:

<?php short_title(30); ?>

Change the “30” to whatever number of characters to be used for your post titles.

Learn more

WordPress Themes In Depth


Show Support

Like our new Facebook Page to show support!