Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save besimhu/366115ab9e952b1b15dc to your computer and use it in GitHub Desktop.
Save besimhu/366115ab9e952b1b15dc to your computer and use it in GitHub Desktop.
Generate navigation based off parent, no matter if you are viewing children or grand children pages.
<nav class="sub-menu-wrap">
<ul class="sub-menu">
<?php
$children = wp_list_pages('depth=1&title_li=&child_of='.$post->ID.'&echo=0');
$current_page = wp_list_pages('title_li=&include='.$post->ID.'&echo=0');
$grandparent=0;
if($post->post_parent):
$parent = wp_list_pages('title_li=&include='.$post->post_parent.'&echo=0');
$siblings = wp_list_pages('depth=1&title_li=&child_of='.$post->post_parent.'&exclude='.$post->ID.'&echo=0');
$parent_children = wp_list_pages('depth=1&title_li=&child_of='.$post->post_parent.'&echo=0');
$parent_post = get_post($post->post_parent);
$grandparent = $parent_post->post_parent;
$parent_siblings = wp_list_pages('depth=1&title_li=&child_of='.$grandparent.'&echo=0&exclude='.$post->post_parent);
endif;
if(!$post->post_parent && $children):?>
<?php echo $children;?>
<?php elseif($post->post_parent && $children):?>
<?php echo $current_page;?>
<?php echo $children;?>
<?php elseif($grandparent && $post->post_parent && !$children):?>
<?php echo $parent;?>
<?php echo $parent_children;?>
<?php elseif(!$grandparent && $post->post_parent && !$children):?>
<?php echo $parent_children;?>
<?php endif;?>
</ul>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment