Custom WordPress Post Titles

January 1, 2012

For those of you who use WordPress, here’s an easy way to display a custom title on a post, while retaining a SEO-friendly URL and controlling your internal anchor text links separately!

By default, when you create a new post, WordPress will auto-generate a url for you based on the title that you assign to it. You can then choose to re-name the Post Title or the Post URL at this point. You control the Post Title, which is the same as the (anchor) Text Link when the post displays on the homepage, and you can customize the URL. All standard WordPress behavior.

Why is this important?

From an editorial standpoint, it allows you more control over what is displayed, and how an element interacts with both the layout and the user. For example, depending on your theme, maybe you can’t use descriptive titles because it would wrap to a second line and ruin the layout. With this feature, you can use an abbreviated Post Title to display on your homepage, but use a more descriptive and accurate title on the Post itself.

From an optimization perspective, this grants you control over the 3 most important things for on-page SEO: Internal Anchor Text, Page Title, and URL structure.

Great, How Do I Get Started?

There’s two files to modify: single.php affects the title displayed on the post, while headers.php affects the post document (HTML) title.

In single.php, replace this:
<?php the_title(); ?>

With this: *May need modification
<?php $alt_title = get_post_meta($post->ID, "alt_title", true); ?>
<?php echo ($alt_title) ? "$alt_title" : the_title(); ?>

This is a PHP if/else statement which checks if the Custom Field alt_title has a value. If a value is set, use it for the post’s title. Otherwise, use the original post title.

In header.php, replace this:
wp_title( '|', true, 'right' );

With this: *May need modification
$alt_title = get_post_meta($post->ID, "alt_title", true);
echo ($alt_title && is_single()) ? "$alt_title - " : wp_title( '|', true, 'right' );

This is another PHP if/else statment which basically checks what is being displayed. If a value is set and a single post is displayed, then use the custom title. Otherwise, use the original page or post title.

That’s all there is to it! Two small tweaks that give you a whole lot of control over very important on-page SEO elements. Best of all, you control everything in the native WordPress interface. No plug-in updates to worry about, no additional features that clutter things up, and best of all: no cost!

Update (1/17/2013)
Here’s how you use alt_title when you create/edit your post:

codewordpress

Next Post