• working on a site where the user wants a side menu “secondary navigation”

    What I need is to list the PARENT and the Children for the current page, even if it is a child and not the parent.

    So for instance I need something like

    PARENT
    child1
    child2
    child3

    That displays just like this even if you’re on Child3 page.

    Hope this makes sense.
    I looked up some stuff but I couldnt get it to work

    I tried this

    <?php
    // Grab Correct Menu
    if ($post->post_parent == 0):
    	$menu_id = $post->ID;
    	$menu_title = $post->post_title;
    else:
    	$menu_id = $post->post_parent;
    	$parent = get_post($menu_id);
    	$menu_title = $parent->post_title;
    endif;
    
    ?>

    and

    <div class="secondnav">
    <h3 class="secondHeader">
    <a href="?page_id=<?php echo $menu_id; ?>">
    <?php echo $menu_title; ?>
    </a>
    </h3>
    
    <ul class="secondMenu">
    <?php wp_list_pages('title_li=&child_of=' . $menu_id); ?>
    </ul>
    	</div>

    but I’m getting a list of all the pages.

Viewing 15 replies - 1 through 15 (of 15 total)
  • Thread Starter ymous anon

    (@chicagodunn)

    I tried this:

    List subpages even if on a subpage
    The above examples will only show the children from the parent page, but not when actually on a child page. This code will show the child pages, and only the child pages, when on a parent or on one of the children.
    
    This code will not work if placed after a widget block in the sidebar.
    
    <?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) { ?>
      <ul>
      <?php echo $children; ?>
      </ul>
      <?php } ?>

    but it’s showing ALL of the parents and children and not just the current…

    Try example 3.

    Thread Starter ymous anon

    (@chicagodunn)

    By Example 3 you mean the third one under the link provided?

    It’s still printing all pages and children.

    Actually no – my example count was wrong. I meant this one:

    <?php
      if($post->post_parent) {
      $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
      $titlenamer = get_the_title($post->post_parent);
      }
    
      else {
      $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
      $titlenamer = get_the_title($post->ID);
      }
      if ($children) { ?>
    
      <h2> <? echo $titlenamer ?> </h2>
      <ul>
      <?php echo $children; ?>
      </ul>
    
    <?php } ?>

    I’ve also used the following function with some success:

    // Return page tree
    	function theme_page_tree($this_page) {
    		$pagelist = '';
    		if( !$this_page->post_parent ) {
    			$children = wp_list_pages('title_li=&child_of='.$this_page->ID.'&echo=0');
    			if( $children ) {
    				$pagelist .= '<li class="current_page_item"><a href="'.  get_page_link($this_page->ID) .'">' . $this_page->post_title . '</a>';
    				$pagelist .= '<ul>' . $children . '</ul>';
    				$pagelist .= '</li>';
    			}
    		}
    		elseif( $this_page->ancestors ) {
    			// get the top ID of this page. Page ids DESC so top level ID is the last one
    			$ancestor = end($this_page->ancestors);
    			$pagelist .= wp_list_pages('title_li=&include='.$ancestor.'&echo=0');
    			$pagelist = str_replace('</li>', '', $pagelist);
    			$pagelist .= '<ul>' . wp_list_pages('title_li=&child_of='.$ancestor.'&echo=0') .'</ul></li>';
    		}
    		return $pagelist;
    	}

    called within the Loop using <?php if( function_exists ('theme_page_tree') echo theme_page_tree($post);?>

    Thread Starter ymous anon

    (@chicagodunn)

    <h2> <? echo $titlenamer ?> </h2> is working but the other half is listing all the pages still.

    Does it require the function?

    As for the function, where should I place this code? How should I place it?
    You lost me a bit on the “within the loop” bit.

    Sorry. Place the function in your theme’s functions.php file first.

    Next – where exactly do you want these sub-pages listed? Within the main page content or elsewhere?

    Thread Starter ymous anon

    (@chicagodunn)

    I’m placing it on the “Pages”.

    I’ve got sort of a custom layout I’m building where this rests on the side of all of the pages.

    Also, when I stated titlenamer was working, I actually was incorrect, it’s displaying the page title, not the title of the parent.

    What I need is a menu that looks like this no matter which child page you’re on:

    PARENT
    ____________
    CHILD
    CHILD
    CHILD
    CHILD

    Is that sub-page list within the page content/Loop? Or is it meant to be in the sidebar? The second option is going to be a bit more complicated.

    Thread Starter ymous anon

    (@chicagodunn)

    I’m using genesis framework theme and placing the code in “framework.php” because that is where the page layout is. It’s not a sidebar, more like a custom div spot (on the page) for this item.

    I don’t know Genesis, I’m afraid. If this block is inside the Loop, you should just be able to add if( function_exists ('theme_page_tree') echo theme_page_tree($post);. Otherwise, you’ll need to expand it a little to grab the current page id.

    Thread Starter ymous anon

    (@chicagodunn)

    How do I know if it’s “inside the loop?”

    See http://codex.wordpress.org/The_Loop for details of how to identify the Loop.

    Thread Starter ymous anon

    (@chicagodunn)

    It is NOT inside the loop, from what I’m understand.

    Thread Starter ymous anon

    (@chicagodunn)

    Bascially what I have is

    data being called up in a loop, then the page layout with css, then more data being called up.

    The part I’m dealing with is not inside of a loop.

    I tried all of the codes on the codex page and all of them just end up listing all of the pages. I’m not sure what is going on.

Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘List Parent and Children’ is closed to new replies.