More WordPress title modification

Some time ago I wrote about re-working WordPress titles to make them easier to read, and more search engine-friendly.

Since then I’ve done a bit more work on WordPress, and so I thought I’d share a few WordPress title modications I’ve used in a recent project.

I’m also going to throw in some <h1> modifications, since <h1> and <title> contents are fairly similar.

The <title>

Read this first.

You got that? Good.
Okay, so what I did was take the reshuffled code, as follows:

<title> <?php wp_title(' '); ?> <?php if(wp_title(' ', false)) { echo ' - '; } ?> <?php bloginfo('name'); ?> </title>

… and prepended a piece of code to check if this is a search page, and if so, show the search query, like this: <?php if (is_search()){ ?> Search for "<?php the_search_query(); ?>" - <?php } ?>

So we get:

<title> <?php if (is_search()){ ?> Search for "<?php the_search_query(); ?>" - <?php } ?> <?php wp_title(' '); ?> <?php if(wp_title(' ', false)) { echo ' - '; } ?> <?php bloginfo('name'); ?> </title>

So if you did a search for “free iPhones”, your browser’s title bar would show:

“Search for “Free iPhones” – Your Site Name”

It’s nothing amazing, but just makes the <title> that much more useful.

The <h1>

This is a unique title for a given page.
There should be — according to me, anyway — just one <h1> element per page.

Here’s some code to look at:

<h1><?php if (!is_home()) { ?><a href="<?php echo get_bloginfo('url'); ?>" title="Back to the home page"><?php if (is_search()){ ?> Search for "<?php the_search_query(); ?>" - <?php } ?><?php wp_title(' '); ?><?php if(wp_title(' ', false)) { echo ' - '; } ?><?php bloginfo('name'); ?></a><?php } else { bloginfo('name'); } ?></h1>

Well, that’s rather hard on the eyes, isn’t it? One day I’ll move this blog off the default WordPress template and onto something that makes code chunks look better.

It’s all wrapped in an ‘if’ statement that checks to see if this is the home page. If it is, then the <h1> contains just the name of the site:
bloginfo('name');

If this is not the home page, then the <h1> element contains the page title as per the <title> element above, wrapped in an <a> element that links back to the home page:
<?php if (!is_home()) { ?><a href="<?php echo get_bloginfo('url'); ?>" title="Back to the home page"> ... title code ... </a><?php } else { bloginfo('name'); } ?>

So there you have it.

Again, I’m sorry this is so hard to read; maybe one day I’ll go into a bit more detail.
Let me know if anything here requires further clarification.

This entry was posted in Coding, Wordpress. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *