How to customize h1 and title tags in WordPress

WordPress has an annoying habit of creating <h1> tags based on whatever you named the post/page.  But sometimes we find ourselves wanting to customize the heading for SEO reasons or even remove it altogether.  Here’s a simple trick to customize the <h1> tags in WordPress without annihilating the original code.

Log into the admin panel and navigate to Appearance > Editor.  The file you need to edit depends on your theme, but it is almost always something like page.php or content-page.php.

Find the line that reads:

<h1><?php the_title(); ?></h1>

As you can see, WordPress simply fetches the title of the entry and slams it into an h1 tag.  What we would like to do is customize the <h1> tag using a custom field called custom_h1.  To do this, replace the existing code with:

<h1>
<?php $thisPost = $post -> ID; if (get_post_meta($thisPost, custom_h1, true) != "") {
echo (get_post_meta($thisPost, custom_h1, true));
} else {
the_title();
}
?></h1>

What this piece of code does is checks for the existence of a custom field called custom_h1.  If it exists, it gets put into the <h1> tag.  If not, WordPress defaults to the entry-title it would have used anyways.  The same technique can be used for page titles as well:


<title><?php
$thisPost = $post -> ID;
if (get_post_meta($thisPost, custom_title, true)) {
echo (get_post_meta($thisPost, custom_title, true)), " | ", bloginfo( 'name' );
} else {
global $page, $paged;
wp_title( '|', true, 'right' );
bloginfo( 'name' );
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) )
echo " | $site_description";
if ( $paged >= 2 || $page >= 2 )
echo ' | ' . sprintf( __( 'Page %s', 'twentyeleven' ), max( $paged, $page ) ); }
?></title>

Need a better web site?

Show that your company is better than the competition by kicking your website up a notch. Contact us if you are ready to take your online brand to the next level.

Leave a Reply

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