List all pages and children in separate ul

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

Read More

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.

Related posts

Leave a Reply

1 comment

  1. 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