0

Using page list with parent, child and grandchild pages for current nav section as widget in sidebar. Parent page displays correctly as widget title when on child page, but when on grandchild page widget title changes to child page title. Regardless of what I've tried to get back to the root parent, the result is always the same as using this:

php global $post; $thispage = $post->ID;
php $parent_title = get_the_title($post->post_parent); echo $parent_title; 

Thanks in advance.

4

2 回答 2

1

我找到了答案,至少就我的目的而言:

php echo get_page(array_pop(get_post_ancestors($post->ID)))->post_title; 
于 2012-04-23T14:56:30.243 回答
0

更新:所以当 Wordpress 已经在其框架中内置了Get Ancestors功能时,我似乎已经开始并重新发明了轮子(很糟糕,我可以补充一下)。使用它非常简单。这应该做你想要的:

if($ancestors = array_reverse(get_ancestors(get_the_ID(), 'page')))
    echo get_the_title($ancestors[0]);
else
    echo get_the_title(get_the_ID());
于 2012-04-21T02:56:42.153 回答