WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have one page with custom posts. They are shown in one page with a list, you can choose one page and when you get to that page I need to show subpages of that page.

Here is so far ive made it, its the first page with the custom posts:

  <select name="select_page" id="select_page" class="select_page" data-mini="true" data-theme="c">

        $pageId = 5;
        $subpages = get_pages( array(
        'child_of' => $pageId,
        'sort_order' => 'asc',
        'sort_column' => 'menu_order') );

    <?php if($post->post_parent) {?>
    <option value="<?php echo get_page_link($pageId) ?>" >Startpage</option>
    <?php } else {?>
    <option value="<?php echo get_page_link($pageId) ?>" selected="selected">Startpage</option>
    <?php } ?>

That was for the list, I need to show subpages of that subpages and show it in the list also.

    foreach($subpages as $page) { $i++;
    $pages[] += $page->ID;      
    <?php if($page->post_title == get_the_title()) {?>
    <option value="<?php echo get_page_link($page->ID) ?>" selected="selected"><?php echo $i.'. '.$page->post_title ?></option>
    <?php } else {?>
    <option value="<?php echo get_page_link($page->ID) ?>"><?php echo $i.'. '.$page->post_title ?></option>  <?php } ?>
    <?php }?> 


My question is:

How can I show subpages of that subpage ?

share|improve this question
up vote 1 down vote accepted

The problem is here

$subpages = get_pages( array(
'child_of' => $pageId,
'sort_order' => 'asc',
'sort_column' => 'menu_order') );

'child_of' parameter also queries for the grandchildren along with children. Use the 'parent' parameter instead which will only query for the direct children. Once this is done, you'll use the same code in your foreach again

foreach($subpages as $page) {
    $subsubpages = get_pages( array(
    'parent' => $page->ID,
    'sort_order' => 'asc',
    'sort_column' => 'menu_order') );

    // use $subsubpages along with $page here

If you want to go even lower in levels, you will probably want to implement this logic in the form of a recursive function

share|improve this answer
Sorry i didnt really get you there ? Should i do that in the same template or new one ? – M3o Nov 2 '12 at 14:29
in the same template. The first one is copied from your code, replace child_of with parent there. The second one should go inside your foreach loop – Mridul Aggarwal Nov 2 '12 at 17:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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