Echo the subtitle of a PARENT page within WordPres

2019-08-11 18:43发布

问题:

I'm attempting to fetch the subtitle from the PARENT page and echo the text within a title tag.

Function:

<?php
    $id_to_get = $post->ID;
    if ( $post->post_parent ) {
        $id_to_get = $post->post_parent;
    }
    $parent = $id_to_get;

    $title = get_the_title($parent);
    $subtitle = get_post_meta($id_to_get, '_base_page_subtitle', true);
?>

HTML Implementation: ...title="<?php echo $subtitle; ?>">

Unfortunately, it is not successfully grabbing the subtitle text. I'm able to populate the title echoing the variable $title, just not with the variable $subtitle.

The value for the Subtitle option is stored within the table "_base_page_subtitle"; I'm using options framework.

I know my issue is with the if statement, however, being new to PHP, I'm having a bit of difficulty figuring out how to capture the data and display it properly.

Thanks again of time!

回答1:

you can do it without a function - just echo it straight out

title="<?php if ( $post->post_parent ) { echo get_post_meta($post->post_parent, '_base_page_subtitle', true);}else{echo get_post_meta($post->ID, '_base_page_subtitle', true);}?>"