WP-Mix

A fresh mix of code snippets and tutorials

Custom title length

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

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress