Skip to content

Instantly share code, notes, and snippets.

@ivandoric
Created April 23, 2014 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivandoric/11212154 to your computer and use it in GitHub Desktop.
Save ivandoric/11212154 to your computer and use it in GitHub Desktop.
wordpress: Dynamical wordpress pages menu with included ancestor
<?php //ADD PAGES MENU
$children = get_pages('child_of='.$post->ID);
$ancestors = get_post_ancestors($post->ID);
//Check if the page has children or ancestors if yes, output pages menu.
if(!empty($children) or !empty($ancestors)){
if( is_page() ) {
global $post;
$parents = get_post_ancestors( $post->ID );
$id = ($parents) ? $parents[count($parents)-1]: $post->ID;
$parent = get_page( $id );
//Variables for adding ancestor page to the menu
$currentUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$parentUrl = get_permalink($id);
$parentTitle = get_the_title($id);
}
$args = array('child_of' => $id, 'title_li'=> '');
?>
<ul class="product-list">
<li <?php /*Check if is current page*/ if($parentUrl == $currentUrl){echo 'class="active"';} ?> >
<a href="<?php echo $parentUrl; ?>"><?php echo $parentTitle; ?></a>
</li>
<?php /*List subpages*/ wp_list_pages( $args ); ?>
</ul>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment