Question

I wrote the following function in WordPress:

function custom_meta_title($default) {
global $post;
$meta = get_post_meta( $post->ID, 'meta_title', true );
if ($meta == '') {
    echo $default;
    } else {
        echo $meta;
    }
}

And here is how I call it in the header.php file:

<title><?php custom_meta_title('A default title tag'); ?></title>

When you create a new post, there is a meta box where you enter the page title for SEO purposes. If you leave it blank, a default page title is displayed instead. When you are on the home page, the default page title is shown.

However, I just discovered that the home page isn't showing the default title like it should; instead, it's showing the title tag of the most recent post. Any idea what could be causing this or how I can fix it?

Was it helpful?

Solution

You have to detect if it's the front page or homepage, maybe some other conditions too, see Conditional Tags.

if( empty($meta) || is_home() || is_front_page() )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top