WordPress.org

Ready to get started?Download WordPress

Forums

Page-specific sub page navigation (2 posts)

  1. dgracie
    Member
    Posted 4 years ago #

    Hi,

    I'm trying to display sub navigation in my sidebar that shows the current page with a "current" class but this is indented beneath its parent link with its sibling links. But also I would like to display the parents siblings links too. So, essentially I want a parent > child > grandchild hierarchy navigation, but I can't figure out the code for this. I have this so far:

    <?php if ( is_page() ) { ?>
      <?php
                if($post->post_parent) {
                $children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0');
                }else{
                $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
                }
                if ($children) {
                $permalink = get_permalink($post->post_parent);
    			?><a href='<?php echo $permalink;?>'>
                <h2>
                <?php
                $parent_title = get_the_title($post->post_parent);
                echo $parent_title;
                ?>
                </h2></a>
                <ul class="sub-nav">
                <?php echo $children; ?>
                </ul>
    
     <?php } } ?>

    but if I am on the grandchild page, it only shows the child page as its parent page. I want it to still show the grandparent and the grandparents sibling links.

    Sorry if this is quite confusing. :/

    (I'll upload images of the output I'm looking for if that will help...)

  2. mkimitch
    Member
    Posted 3 years ago #

    This is what I've used to accomplish what I think you are describing. Hope it helps!

    <?php
    if ($post->post_parent) {
    	//get the parrent and see if it'a a top page (has no parent)
    	$parent = get_page($post->post_parent);
    	if ($parent->post_parent) {
    		//if it's not a top page, then his parent should be
    		$children = wp_list_pages('title_li=&child_of=' . $parent->post_parent . '&echo=0');
    	} else {
    		$children = wp_list_pages('title_li=&child_of=' . $post->post_parent . '&echo=0');
    	}
    } else {
    	$children = wp_list_pages('title_li=&child_of=' . $post->ID . '&echo=0');
    }
    if ($children) { ?>
    	<ul class="side-nav">
    	<?php
    		if ($post->ancestors[1]) {
    			$ancestor_title = get_the_title($post->ancestors[1]);
    			$ancestor_link = get_permalink($post->ancestors[1]);
    		?>
    		<li><a href="<?php echo $ancestor_link; ?>"><?php echo $ancestor_title; ?></a></li>
    		<?php } elseif ($post->post_parent) {
    			$parent_title = get_the_title($post->post_parent);
    			$parent_link = get_permalink($post->post_parent);
    		?>
    		<li><a href="<?php echo $parent_link; ?>"><?php echo $parent_title; ?></a></li>
    		<?php } else { ?>
    		<li class="current_page_item"><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php } echo $children;?>
    	</ul>
    <?php } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic