创建了一个侧栏,其中列出了WP的默认页面的子级。然而,有一些断断续续的行为,边栏在一个页面上正确列出了目录,而在另一个页面上却什么也没有列出。
可以看到充分发挥作用的实例here; 在ancestor page here; 以及祖先侧边栏标题出现的位置,但是not the child pages.
因为它在某些页面上工作,但在其他页面上没有,所以我真的不确定到底发生了什么。它们都是相同的模板并调用相同的导航。
我的代码如下:
<?php ( is_page() ) {
global $post;
$ancestors = get_post_ancestors( $post->ID ); // Get the array of ancestors
//Get the top-level page slug for sidebar/widget content conditionals
$ancestor_id = ($ancestors) ? $ancestors[count($ancestors)-1]: $post->ID;
$the_ancestor = get_page( $ancestor_id );
$ancestor_slug = $the_ancestor->post_name;
//If there are no ancestors display a menu of children
if (count($ancestors) == 0 && is_front_page() == false || is_page(\'hammond-society\') ) {
$page_name = $post->post_title;
$test_menu = wp_nav_menu( array(
\'theme_location\' => \'main_nav\',
\'menu_class\' => \'nav\',
\'container_class\' => \'offset-gutter\',
\'items_wrap\' => \'<div class="radius-topright" id="sidebar_header"><h5 class="white">Also in <span class="grey bold">\' . $page_name . \'</span></h5></div><ul class="%2$s">%3$s</ul>\',
\'submenu\' => $page_name,
\'depth\' => 1,
\'echo\' => false
));
if (strpos($test_menu,\'<li id\') !== false) : echo $test_menu; endif;
}
//If there are one or more display a menu of siblings
elseif (count($ancestors) >= 1) {
$parent_page = get_post($post->post_parent);
$parent_url = $parent_page->guid;
$parent_name = $parent_page->post_title;
?>
<div class="offset-gutter radius-topright" id="sidebar_header">
<h5 class="white">Also in <a href="<?php echo $parent_url;?>" class="grey bold"><?php echo $parent_name ?></a></h5>
</div>
<?php
wp_nav_menu( array(
\'theme_location\' => \'main_nav\',
\'menu_class\' => \'nav\',
\'container_class\' => \'offset-gutter\',
\'submenu\' => $parent_name,
\'depth\' => 1
));
}
}
?>