This is the code from the codex most commonly used for sidebar menus. It displays the child pages of the top level ancestor and the ancestor itself.
There is one thing about this code I would like to change – I don’t want the top level ancestor displayed if it has no children! It doesn’t make much sense to have a one link menu which points to itself.
How can I edit this code to achieve this? I’ve previously asked a similar question, but was never given the correct answer. All the answers given also listed children of the parent’s child (the second level child page).
In functions.php
// Sub-pages menu
if(!function_exists('get_post_top_ancestor_id')){
/**
* Gets the id of the topmost ancestor of the current page. Returns the current
* page's id if there is no parent.
*
* @uses object $post
* @return int
*/
function get_post_top_ancestor_id(){
global $post;
if($post->post_parent){
$ancestors = array_reverse(get_post_ancestors($post->ID));
return $ancestors[0];
}
return $post->ID;
}}
Calling it:
<ul class="subpages">
<?php wp_list_pages( array('title_li'=>'','include'=>get_post_top_ancestor_id()) ); ?>
<?php wp_list_pages( array('title_li'=>'','depth'=>1,'child_of'=>get_post_top_ancestor_id()) ); ?>
</ul>
This code checks if a page has children, I’m curious if this could be merged with the codes above to create the correct code:
$children = get_pages('child_of='.$post->ID);?>
if( count( $children ) != 0 ) { show list as normal }
else { show "no parent" text }
Personally, I think this is the neatest, least intensive (database querying) solution:
This may not be exactly what you’re looking for but I use a Frankenstein approach, it gets the job done:
First, let’s check if we even need to show subpages, this will make sure this page either has children or is a child and we’re not on a 404 page – if true, we will get the ancestor (parent) ID, then run WP_List_Pages twice so that we show the parent and children all in the same list.
Into your
functions.php
File