Hey everyone! I’m having trouble with my WordPress site (version 3.3.1). The wp_title
function works fine on all pages except the homepage. There, it returns nothing. I’ve tried looking for solutions, but most are outdated.
Here’s the code I’m using:
<title><?php wp_title('| My Awesome Blog', true, 'right'); ?></title>
It works everywhere but the main page. I don’t want to hardcode the title. Any ideas what I might be doing wrong? I’m pretty new to WordPress, so I’m probably missing something obvious. Thanks for any help!
I’ve encountered this issue before, and it can be frustrating. One thing that worked for me was using the ‘wp_title_parts’ filter to modify the title on the homepage. Here’s what I did:
function custom_wp_title( $title ) {
if ( is_home() || is_front_page() ) {
$title = get_bloginfo( 'name' );
$description = get_bloginfo( 'description', 'display' );
if ( $description ) {
$title .= ' - ' . $description;
}
}
return $title;
}
add_filter( 'wp_title_parts', 'custom_wp_title' );
This approach lets you customize the homepage title without hardcoding it. It pulls in your site name and description dynamically. You might need to adjust it based on your specific needs, but it’s a good starting point. Hope this helps!
Have you considered using the ‘pre_get_document_title’ filter? It’s a more modern approach that works well with newer WordPress versions. Here’s a snippet you can try:
function custom_homepage_title( $title ) {
if ( is_home() || is_front_page() ) {
$title = get_bloginfo(‘name’) . ’ | ’ . get_bloginfo(‘description’);
}
return $title;
}
add_filter( ‘pre_get_document_title’, ‘custom_homepage_title’ );
This should override the default title behavior on your homepage. It combines your site name and tagline, but you can adjust it as needed. Remember to clear your cache after making changes. Also, ensure your theme isn’t overriding title functionality somewhere else.
hey neo_stars, sounds like a common issue. have u checked ur homepage settings? sometimes wp uses a static page as the homepage, which can mess with wp_title. try adding is_home() check in ur header.php:
<?php if (is_home()) { bloginfo('name'); } else { wp_title('| My Awesome Blog', true, 'right'); } ?>
that might do the trick. lmk if it helps!