3

I've been trying to do this all weekend, I'm going round in circles now.

I want to list all pages and their children in separate ul's

The page setup is like this

    HOME        ABOUT         CONTACT
                Who we are    Where we are 
                What we do    How to find us
                How we do it  

I want to output it like this

    <ul>
      <li>Home</li>
    </ul>

    <ul>
      <li>About</li>
      <li>Who we are</li>
      <li>What we do</li>
      <li>How we do it</li>
    </ul>

    <ul>
      <li>Contact</li>
      <li>Where we are</li>
      <li>How to find us</li>
    </ul>

This will give me the top pages in separate ul's

    <?php
      $args = array(
        'sort_column' => 'menu_order',
        'parent' => 0,
      );
      $pages = get_pages($args);
      foreach($pages as $page){
        ?>
        <ul>
          <li>
            <?php 
                        echo $page->post_title;
                    ?>  
          </li>
        </ul>
        <?php
      }

    ?>

I'm now trying to add the child pages to the ul

I'm thinking something like this but this gives me all the pages in each ul

    <?php
      $args = array(
        'sort_column' => 'menu_order',
        'parent' => 0,
      );
      $pages = get_pages($args);
      foreach($pages as $page){
        ?>
        <ul>
          <li>
            <?php 
                        echo $page->post_title;
                        wp_list_pages('title_li=&depth=0&child_of'.$page->ID.'');
                    ?>  
          </li>
        </ul>
        <?php
      }

    ?>        

Is there a way to say all children of this page.

Any help would be greatly appreciated.

1 Answer 1

3

your code is missing the = after child_of

<?php
$args = array(
    'sort_column' => 'menu_order',
    'parent' => 0,
    );
$pages = get_pages($args);
foreach($pages as $page){
    ?>
    <ul>
        <li>
        <?php 
        echo '<a href="' . get_permalink( $page->ID ) . '">' . $page->post_title . '</a>';
        ?>  
        </li>
        <?php  
        wp_list_pages('title_li=&depth=0&child_of='.$page->ID.'');
        ?>  
    </ul>
    <?php
    }
?> 

if you want to make sure to only show one level of child pages, consider to set the 'depth' parameter to 1

3
  • Thanks Michael, best part of a day messing with this and I was missing a "="
    – user668499
    Feb 25, 2013 at 9:15
  • Might have spoken too soon. The first echo echo $page->post_title; just gives me text of the page but I need that to be a link as well
    – user668499
    Feb 25, 2013 at 9:27
  • edited to show parent pages as link. this is the edited line: echo '<a href="' . get_permalink( $page->ID ) . '">' . $page->post_title . '</a>';
    – Michael
    Feb 25, 2013 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.