0

I'm using a function to print the titles for every type of content. I also configured a new metabox in the posts and pages to enter the title tag.

This is part of the function to print the titles:

//...
elseif (is_page() || is_single()) { //Page and Post Title
    global $post;
    $metatitle = get_post_meta($post->ID, 'metatitle_value_key', true);
    if ($metatitle) {
        echo stripslashes($metatitle);
    } else {
        the_title();
    }
}
//...
else { wp_title(''); }

It works perfect for posts and pages, except when I select a static home page and the blog page (it's a blank page created previously). Thanks to the last else it prints the normal title of the page 'Blog', but it doesn't print the metabox with the metatitle 'My metatitle box'.

I thought it was 'cause of the post ID, so I separeted is_page() and is_post() trying to get the page ID out of the loop, with this code $page_id = get_queried_object_id(); but this one didn't work for me. The following code is what I have:

//...
elseif (is_page()) { //Page Title
    global $post;
    $page_id = get_queried_object_id();
    $metatitle = get_post_meta($page_id, 'metatitle_value_key', true);
    if ($metatitle) {
        echo stripslashes($metatitle);
    } else {
        the_title();
    }
} //...

What can I try to do that? Probably the problem is due to the way to insert the metabox, it uses $post_id every time. Anyway I'm not sure, 'cause it works for both: posts and pages.

1
  • Which template file is your code in? May 6, 2014 at 18:45

1 Answer 1

0

Ok, I found the method by myself. This is the code:

//...
elseif (is_page() || is_single()) { //Page and Post Title
    global $post;
    $metatitle = get_post_meta($post->ID, 'metatitle_value_key', true);
    if ($metatitle) {
        echo stripslashes($metatitle);
    } else {
        the_title();
    }
}  elseif (is_home()) { //Using is_home for the blog page

    //And using get_queried_object        
    $page_object = get_queried_object();
    $metatitle = get_post_meta($page_object->ID, 'metatitle_value_key', true);
    if ($metatitle) {
        echo stripslashes($metatitle);
    } else {
        the_title();
    }
}
//...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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