Hey Tara,
WordPress recently changed the best practice for the way themes add the title tag, and the function above is the fallback for pre-4.1 installations. If you’re running WordPress 4.1, it won’t have any affect.
What you’re looking for is the wp_title() filter. You can use that to modify the title that is output.
Thanks, can’t find the code for that. What file should I be looking in? If possible I’d like to keep the changes in the child theme.
You can attach a function to the wp_title() filter to change the title text like this:
function my_title_filter( $title ) {
$title = get_bloginfo( 'name' ) . ' | ' . $title;
return $title;
}
add_filter('wp_title', 'my_title_filter', 99);
Once you add that function to your child theme, it will run with the rest of your code. By attaching a function to the wp_title hook, WordPress will run that function every time the wp_title hook is called.
The end result is that every time WordPress generates the text for a title tag, this function will take the current title, and replace it with the blog name followed by a divider followed by the existing title. It will then return that modified title.
Oh very good thanks. That’s working perfectly for everything but the home page (a static front page) which is displaying “blogname | blogname| blogdescription”.
Yea no problem! To set the title differently on just the homepage, use the following modification of that function:
function my_title_filter( $title ) {
if( is_front_page() ) {
$title = $title;
} else {
$title = get_bloginfo( 'name' ) . ' | ' . $title;
}
return $title;
}
add_filter('wp_title', 'my_title_filter', 99);
I’m not sure what you want the title to be on the homepage, so I just left $title set as itself. You can change that whatever you’d like.