AnswerBun.com

How to get the meta title of a page configured as blog (loop)

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.

One Answer

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

Add your own answers!

Related Questions

CREATE TABLE with dbDelta does not create table

1  Asked on October 30, 2021 by fcd

     

how to add shortcode widget at end of main blog page

1  Asked on October 30, 2021 by shubhamk

 

Placing background image over section divider

1  Asked on October 30, 2021 by user191925

   

jQuery Click event is not fired at all

1  Asked on October 30, 2021 by islam-hanafi

   

Make the product page as homepage WooCommerce

0  Asked on October 30, 2021 by gabriel-borges

     

How to be sure to get last page version without ANY cache?

2  Asked on October 30, 2021 by j-bizmai

   

How to grab posts in Gutenberg Block?

1  Asked on October 30, 2021 by stephen-sabatini

 

Custom post type permalink returns bad url

1  Asked on October 30, 2021 by ncti

     

Menus don’t appear in admin area / WordPress Multisite

0  Asked on October 30, 2021 by user191642

   

Client side HTTP parameter pollution (reflected)

0  Asked on October 30, 2021 by divided

 

WP Ajax Function Always Returning undefined

1  Asked on October 30, 2021 by brian-bruman

     

Group Posts by Category in Monthly Archive

2  Asked on October 30, 2021 by zero-goki

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP