Time for action – optimizing our theme's page meta tags

The meta tags go in the <head> section of each page, which in a WordPress theme is in header.php.

  1. Open your theme's header.php file.
  2. Find the following code and delete it:
    <meta name="description" content="Description of content that contains top keyword phrases"/>
    <meta name="keywords" content="Key words and phrases, comma separated, not directly used in content - google ignores this tag but used in other engines as a fall back"/>
    <title>Open Source Online Magazine</title>
  3. Add an optimized title meta tag. In the space where you've just deleted the existing code, add the following:
    <title><?php wp_title('|','true','right'), ?><?php bloginfo('name'), ?></title>
  4. Now, below that, add the code for the description:
    <meta name="description" content="<?php bloginfo('description'), ?>" >
  5. Save your header.php file and switch to your browser. Visit the page for one of your blog posts and take a look at the title displayed.

What just happened?

We added some meta tags to our theme, specifically the title tag and a description meta tag. Let's check the code we used:

Our title tag includes two elements:

  • Firstly, the name of the current post or page that is output using wp_title('|','true','right'):
    • wp_title is the WordPress template tag which displays the title of the current post or page
    • The first parameter '|' displays a separator
    • The second parameter, 'true' tells WordPress to display (or echo, in PHP terms) the page title, rather than just fetching it for later use
    • The third parameter tells WordPress where to display the separator—to the right of the page title
  • Next, we use bloginfo('name') to display the site name—it's that handy bloginfo tag again.

Our description meta tag uses the content attribute to display the site description using bloginfo('description')

Tip

Why no meta tag for keywords?

We haven't included a meta tag for keywords as Google no longer uses keywords when generating search results. This is because keywords were so widely abused in the past that they became unreliable. If you wanted to use keywords (for example, if you're optimizing for a search engine that does still use keywords), the best approach is to use a plugin.

So let's have a look at our post, as viewed in the Safari browser:

What just happened?

You can see our title displayed by the browser. However, you can't see the description. To check that, let's look at the code generated for this page:

<title>Post with a shorter title but more text | Open Source Magazine</title>
<meta name="description" content="Using Open Source for work and play" >

As you can see, our description is there too. Well done!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset