WordPress.org

Ready to get started?Download WordPress

Forums

Only show tiers 1 and 2 of navigation (no grandchildre/subsubpages) (4 posts)

  1. artgeek
    Member
    Posted 5 years ago #

    Hi! I have a site with the following set up:

    Parent1
    -Child1a
    Parent2
    -Child2a
    -Child2b
    Parent3
    -Child3a
    -Child3b
    -Child3c
    -GrandChild3c.1
    -GrandChild3c.2
    -Child3d
    Parent4

    Currently, when I am on a Parent or Child page, the submenu navigation lists the Child pages (see code below). When I am on a Grandchild page, the Grandchild pages are listed instead.

    Is it possible to write a PHP "if" statement to not show this third tier of navigation or, ideally, if I were on GrandChild2c.2 page, to list the same navigation I would see if I were on Child2c?

    I am using the following to display the navigation in my header.php :

    <div id="menu">
    	<ul class="caps style<?php echo ($post->post_parent); ?> style<?php the_ID(); ?>"><?php wp_list_pages('include=7,9,11,12&sort_column=menu_order&title_li=&depth=1'); ?></ul>
    	</div>
    	<div id="submenu" class="style<?php echo ($post->post_parent); ?> style<?php the_ID(); ?>">
    	<?php
    if($post->post_parent)
    $children = wp_list_pages("sort_column=menu_order&title_li=&depth=1&child_of=".$post->post_parent."&echo=0"); else
    $children = wp_list_pages("sort_column=menu_order&title_li=&depth=1&child_of=".$post->ID."&echo=0");
    if ($children) { ?>
    		<ul>
    		<?php echo $children; ?>
    		</ul>
    <?php } ?>
    	</div>
  2. artgeek
    Member
    Posted 5 years ago #

    Figured it out; this gave the result I was looking for:

    <div id="submenu">
    <?php
    if($post->post_parent) {
    	$parent = get_post($post->post_parent);
    	if ($parent->post_parent) {
    		$children = wp_list_pages("depth=1&sort_column=menu_order&title_li=&child_of=".$parent->post_parent."&echo=0");
    	} else {
    		$children = wp_list_pages("depth=1&sort_column=menu_order&title_li=&child_of=".$post->post_parent."&echo=0");
    	}
    }
    else
    	$children = wp_list_pages("depth=1&sort_column=menu_order&title_li=&child_of=".$post->ID."&echo=0");
    if ($children) {?>
    	<ul><?php echo $children;?></ul>
    <?php }?>
    	</div>
  3. nathan12343
    Member
    Posted 5 years ago #

    This is great. I have been looking for exactly this solution. Thanks for following up on your own question!!

  4. micasuh
    Member
    Posted 4 years ago #

    Yay, this is what I needed too except, I didn't want anything showing on the parent pages. So, I modified the code to look like this:

    <div id="submenu">
    <?php
    if($post->post_parent) {
    	$parent = get_post($post->post_parent);
    	if ($parent->post_parent) {
    		$children = wp_list_pages("depth=1&sort_column=menu_order&title_li=&child_of=".$parent->post_parent."&echo=0");
    	} else {
    		$children = wp_list_pages("depth=1&sort_column=menu_order&title_li=&child_of=".$post->post_parent."&echo=0");
    	}
    }
    if ($children) {?>
    	<ul><?php echo $children;?></ul>
    <?php }?>
    	</div>

    Deleting that last else statement keeps the parent from showing any child pages, but children pages will show grandchildren pages and grandchildren pages will show fellow grandchildren pages. Thank you!

Topic Closed

This topic has been closed to new replies.

About this Topic