Created
April 23, 2014 11:46
-
-
Save ivandoric/11212154 to your computer and use it in GitHub Desktop.
wordpress: Dynamical wordpress pages menu with included ancestor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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