Awesome WordPress SEO With Custom Fields

SEO is something I’ve discussed before here on [wp], but here we’re going to dive into a slightly more specific area: post titles, urls and title tags.

Your average WordPress [theme] will take the title you put into the post and output it as the <title>, the post title in archive pages and send it as the title to RSS readers. That’s not really the most exciting way of doing things or the way that offers the most customisation; I want to be able to use a title written for search engines and use that as the <title>, another written for archives perhaps with some emphasis and one final one for RSS readers. With custom fields, it’s easy.

What? If?

First we need a basic understanding. Using something similar to the following we can see if a custom field exists and if it does display it. If it doesn’t, we’ll do something else:

<?php
$name = get_post_custom_values("Custom-Field-Name");
if ( is_array($name) ) { ?>
<?php echo get_post_meta($post->ID, "Custom-Field-Name", true); ?>
<?php }	else { ?>
Do some stuff
<?php }	?>

That’s the fundamental bit of info we need.

Show a specific title to the archives

This one’s fairly simple. We’re going to create a custom field ‘archive-title’ and in that you can type any title you want and if you pop the code in the correct places you’ll be able to use any type of HTML you like – for example your could have:

A title with a bit of <em>emphasis</em>

or even…

A title with some <b>bold!</b>

The code to display the custom field (and falls back to the_title) is as follows:

<?php
$archivetitle = get_post_custom_values("archive-title");
if ( is_array($archivetitle) ) { ?>
<?php echo get_post_meta($post->ID, "archive-title", true); ?>
<?php }	else { ?>
<?php the_title(); ?>
<?php } ?>

A specific title for the <title> tag

It’s pretty much the same for the <title> except you’ll need a different custom field name and obviously the code goes in the header:

<?php
$titletag = get_post_custom_values("tag-title");
if ( is_array($archivetitle) ) { ?>
<?php echo get_post_meta($post->ID, "tag-title", true); ?>
<?php }	else { ?>
<?php the_title(); ?>
<?php } ?>

A specific title for RSS readers

And finally, for RSS readers you can just change the title in the post editor. It’s that simple. With these three bits of info up your sleeve, you can boost for SEO. In theory anyway. At the very least, you can start writing for readers, not search engines.


6 Responses

Comments

Pingbacks