in WordPress

How To Create Title Tag In Wordpress

Wordpress Depending on what page you are on you need to make sure that the title tag is different on each page. This is important for SEO because you can't have pages with the same title tag. As Wordpress is a CMS you use the same header section on each page so you need to add some logic to make sure they are different depending on what page you are on. Here is a Wordpress snippet that you should add to the head tag of your theme.

      if (function_exists('is_tag') && is_tag()) {                    //tag page
         single_tag_title("Tag Archive for &quot;"); echo '&quot; - '; }
      elseif (is_archive()) {                                         //Archive page
         wp_title(''); echo ' Archive - '; }
      elseif (is_search()) {                                          //Search page
         echo 'Search for &quot;'.wp_specialchars($s).'&quot; - '; }
      elseif (!(is_404()) && (is_single()) || (is_page())) {          //Single page
         wp_title(''); echo ' - '; }
      elseif (is_404()) {                                             //404 page
         echo 'Not Found - '; }
      if (is_home()) {                                                //Home page
         bloginfo('name'); echo ' - '; bloginfo('description'); }
      else {                                                          //Blog Page
          bloginfo('name'); }
      if ($paged>1) {                                                 //Paged Search page
         echo ' - page '. $paged; }

This checks to see what type of page you are on and will change the title depending on this. ## Using Filters With Wordpress Title Tag

Instead of using the above snippet to create your title tag you can add a filter to change the contents of the default Wordpress function wp_title(). Here is a code snippet from the Wordpress theme Twenty Twelve and this into the functions.php file.

 * Creates a nicely formatted and more specific title element text
 * for output in head of document, based on current view.
 * @since Twenty Twelve 1.0
 * @param string $title Default title text for current view.
 * @param string $sep Optional separator.
 * @return string Filtered title.
function twentytwelve_wp_title( $title, $sep ) {
    global $paged, $page;

    if ( is_feed() )
        return $title;

    // Add the site name.
    $title .= get_bloginfo( 'name' );

    // Add the site description for the home/front page.
    $site_description = get_bloginfo( 'description', 'display' );
    if ( $site_description && ( is_home() || is_front_page() ) )
        $title = "$title $sep $site_description";

    // Add a page number if necessary.
    if ( $paged >= 2 || $page >= 2 )
        $title = "$title $sep " . sprintf( __( 'Page %s', 'twentytwelve' ), max( $paged, $page ) );

    return $title;
add_filter( 'wp_title', 'twentytwelve_wp_title', 10, 2 );

Having this in the functions.php file will mean that in the header.php file all you need to use is the wp_title() function.

<?php wp_title( '|', true, 'right' ); ?>

Subscribe To The Weekly Newsletter

Get weekly updates to your email