Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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">

    <?php
        $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.

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

    </select>

My question is:

How can I show subpages of that subpage ?

share|improve this question

1 Answer 1

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

 
discard

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.