In this text I’ll present a little snippet which allows to dynamically asign h1/h2 tags to the title of a WordPress blog, depending on the type of page. The code is very simple and intuitive and makes use of conditional tags.
A story about headings
Once upon a time, when I started blogging, there was a strong logic behind the heading distribution on a blog page. The blog title was nested in a h1 tag, and every other title was wrapped by h2 tag. Almost every theme available preserved this structure, proposed by the Classic and Default (Kubrik) themes. But, back then, WordPress was just a blogging platform, and blogs were onlu personal logs. And, for a personal log, it was the best possible distribution.
Next, when the professional blogging sprang into its virtual being, the blog’s name began to lose its importance; the post title was the new star, as it was more likely to be featured by SERPs. The emergence of niche blogs produced an important mutation in the heading structure (as well as in the structure of the title meta tag). The urge of getting individual posts posts indexed fast (and high) convinced pro bloggers to drastically change the headings’ hierarchy. Widget titles were wrapped by h3 tags (which seems to be a good idea, since “Recent comments” or “Blogroll” could hardly be a topic on a blog about tulips), the headings of single post and page titles shifted to h1, and the blog’s name was abandoned to the misery of a h2 tag.
This heading structure, preached by many powerful voices in the wp-sphere, is by now an established coding trend. It can be found in many themes (my own included) and, like any other trend, tends to remain unquestioned.
The problem
The problem with this heading distribution is that it leaves most of the pages without a h1 tag; or, sucha page, is like a book without title. For an information architect, a homepage without a h1 tag is a crime. Indexing robots might have difficulties in finding the most important headline on the page, and they have to only rely on the title tag. This isn’t a drama, but it can be done better.
The solution
Conditional tags are one of the most powerful tools offered by WorsPress. Their reason for being is to allow displaying different content on different type of pages. So why shouldn’t we use them to provide blogs with a proper heading distribution, saving the blog name from oblivion?
The code
<h2><a href="<?php echo get_settings(‘home’); ?>"><?php bloginfo(‘name’); ?></a></h2>
<?php } else { ?>
<h1><a href="<?php echo get_settings(‘home’); ?>"><?php bloginfo(‘name’); ?></a></h1>
<?php } ?>
Explanation
The code is telling WordPress: if it’s a single or a “page” page, nest the blog name in a h2 tag (since the post title is wrapped by a h1 tag); for any other type of page (homepage/catepgory/tag/year/month/day/hour/minute/search), nest the blog name in a h1 tag.
If you want to use to use this code, open header.php, spot the current code for your blog name and replace it (but don’t forget to back it up before doing so).
Published on the 7th of February, 2010, in WordPress · Print
Share this article