0

On the site I am working on, currently page titles have this format:

Page title | Site name

This is fine, but I want the Page title removed from the homepage, as the Google search result for the homepage has 'Home' in it.

So how do I change the title for just the homepage, from...

Home | Site name

to..

Site name

?

This is the relevant part in header.php:

<title><?php


        global $page, $paged;

        wp_title( '|', true, 'right' );

        // Add the blog name.
        bloginfo( 'name' );

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

        // Add a page number if necessary:
        if ( $paged >= 2 || $page >= 2 )
                echo ' | ' . sprintf( __( 'Page %s', 'twentyeleven' ), max( $paged, $page ) );

        ?></title>
1

You can add a check to see if the current page is the home page, and if so skip adding the page title. This would make the final code look like this:

<title><?php

global $page, $paged;

// Add the page title if not the front page
if ( is_front_page() ) {
        wp_title( '|', true, 'right' );
}

// Add the blog name.
bloginfo( 'name' );

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

// Add a page number if necessary:
if ( $paged >= 2 || $page >= 2 )
        echo ' | ' . sprintf( __( 'Page %s', 'twentyeleven' ), max( $paged, $page ) );

?></title>

However, the best thing to do would be to use this code in header.php:

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

And then filter the title using this code in functions.php:

function wpse_132052_wp_title( $title, $sep ) {
    global $paged, $page;

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

    // Use the site name and description for the home/front page
    if ( is_home() || is_front_page() ) {
        $title = get_bloginfo( 'name' );

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

    // Add a page number if necessary:
    if ( $paged >= 2 || $page >= 2  ) {
        echo ' | ' . sprintf( __( 'Page %s', 'twentyeleven' ), max( $paged, $page ) );
    }

    return $title;

}
add_filter( 'wp_title', 'wpse_132052_wp_title', 10, 2 );
  • @paradroid check if force rewrite titles setting is enabled on the Titles & Metas SEO menu. If it is, disable it – shea Feb 1 '14 at 5:04
  • I also needed to remove bloginfo( 'name' ); to stop duplicate site names. – paradroid Feb 1 '14 at 5:32
  • @paradroid try using the new code in the answer. It's the correct method for handling page titles, and should fix any errors with the site title – shea Feb 1 '14 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.