WordPress Development Asked by Gerard on September 8, 2020
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.
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();
}
}
//...
Answered by Gerard on September 8, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP