• Resolved Dave Parker

    (@deapness)


    Hello,

    So I’ve been sitting here throwing everything I can at solving this and I cannot manage to get it done. I am pretty newb @ PHP but I am pretty good @ reading it and putting things where they need to be to work right. However, I cannot figure this one out: I am trying to make a navigation that will show both the Parent AND Children pages for a given parent page.

    Example: On the main navigation you click Parent 1. On the left sidebar this is what I want to display:

    Parent 1
    Child 1
    Child 2
    Child 3

    But what I can only accomplish is:

    Child 1
    Child 2
    Child 3

    With this code (that I got from the codex):

    <?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 } ?>

    I also want to emphasize: I only want to show the parent and sub pages for the current parent page I am on. So if I click on Parent 1, I don’t want to show anything from Parent 2, Parent 3, etc.

    Thanks for the reading and the help!

    Dave

Viewing 9 replies - 1 through 9 (of 9 total)
  • What about using the post title for the title_li

    "title_li=".$post->title ."&child_of

    Thread Starter Dave Parker

    (@deapness)

    Thanks for the post!

    But like I said I am pretty newb @ php but is this what you wanted me to try?

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

    I added that code you posted in after wp_list_pages(. It doesn’t appear to be working, though.

    Thread Starter Dave Parker

    (@deapness)

    Btw, I earlier put something with the_title to make the link.. but it would just show the current pages link. So when I clicked on a Child 1, it would show Child 1 and not Parent 1, if that makes sense lol.

    Well there is this:

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

    Thread Starter Dave Parker

    (@deapness)

    Thanks again Michael for the post!

    However, this code works perfect when I am on a Child page, but when I am on the Parent page, it lists all of the pages (including other parent pages).. twice.

    Also on the 3rd $children I changed “inclue” to “include” .

    Dave

    Didn’t test these as you can tell…that 3rd one should be

    $children = wp_list_pages("title_li=&nclude=".$post->ID."&echo=0");

    Thread Starter Dave Parker

    (@deapness)

    Awesome, it works perfect! Thanks Michael!

    First of all I gotta say this was extremely helpful, kudos to you guys.

    I used this on my site and I also have sub pages of sub pages, which are separated from their parents by hierarchy. So I would like to know is it possible to hide the sub pages of sub pages and then make them appear when you click on parent page? If that made any sense.

    So this is what i have now:

    Parent 1
    Child 1
    Child 2
    Child of a child 1
    Child of a child 2
    Child 3
    Child 4
    Child of a child 3 <– So I would like this to appear when you click on the “Child 4”

    Thanks beforehand.

    MichaelH again shows how he’s a WordPress GURU! Thanks Mike!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘wp_list_pages Parent AND Children Pages’ is closed to new replies.