Echo the subtitle of a PARENT page within WordPress?

冷暖自知 提交于 2019-12-11 08:57:44

问题


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);}?>"


来源:https://stackoverflow.com/questions/18089543/echo-the-subtitle-of-a-parent-page-within-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!