WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Navigation the same on 2nd and 3rd level pages (2 posts)

  1. dcurran
    Member
    Posted 1 year ago #

    I am trying to put together a navigation where the 2nd and 3rd level pages are displaying the same content. I have searched and searched found similar forum questions but none that are exactly the same.

    I found this code which is doing what I need on the 3rd level page.

    <?php
    
    /* if the current pages has a parent, i.e. we are on a subpage */
    if($post->post_parent){
    	$children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); // list the parent page
    	$children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); // append the list of children pages to the same $children variable
    } 
    
    /* else if the current page does not have a parent, i.e. this is a top level page */
    else {
    	$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); // form a list of the children of the current page
    }
    
    /* if we ended up with any pages from the queries above */
    if ($children) { ?>
    	<ul class="submenu">
    		<?php echo $children; /*print list of pages*/ ?>
    	</ul>
    <?php } ?>

    But I want the 2nd level page to also only display it's title and sub pages, so the list would be the same on a level 2 page and a level 3 page. Is there a way to modify this code to target pages with a parent and pages with a grandparent?

    On both the 2nd and 3rd level pages the navigation should be:
    Second level title name
    Subpage of second level 1
    subpage of second level 2

  2. dcurran
    Member
    Posted 1 year ago #

    If anyone else is interested I put something together from a bunch of different pieces of code:

    <?php //parent variables
    	$parent = get_post($post->post_parent);
    	$parent_title = get_the_title($parent);
    	$grandparent = $parent->post_parent;
    	$grandparent_title = get_the_title($grandparent);
    	$current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );?>
    <?php // is the homepage the granparent? = third level page
    if ($grandparent == is_page('0')) {
    	$children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); // list the parent page
    	$children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); // append the list of children pages to the same $children variable
    	if ($children) { ?>
        <ul class="submenu">
          <?php echo $children; /*print list of pages*/ ?>
        </ul>
        <?php }?>
    <?php // is the homepage the parent? = second level page
    	} elseif ($post->post_parent ==is_page('0')) {
    	  $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
    
      if ($children) { ?>
        <ul>
          <li class="current_page_item"><?php echo get_the_title(); ?></li>
          <?php echo $children; ?>
        </ul>
        <?php
    	} else {// your else stuff
    
    	}
    ?>
        <?php // I must be a top level page!
    	} else { ?>
        <!-- no parent to show -->
    
        <?php }?>

Topic Closed

This topic has been closed to new replies.

About this Topic