tags:

views:

76

answers:

2

I want to list all sub-pages only one level though of one particular page. I was reading Function Reference/get pages and thought that $pages = get_pages( array( 'child_of' => $post->ID, 'parent' => $post->ID)) ; will do the trick but it is not working. It lists all pages on the same level like the page I call that code from. If I omit parent option I will get all pages even with sub-pages that I want. But I want only sub-pages.

The whole function is like

    function about_menu(){
    if (is_page('about')){

    $pages = get_pages( array( 'child_of' => $post->ID, 'parent' => $post->ID)) ;
        foreach($pages as $page)
        {       
        ?>
            <h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
        <?php
        }   
    }
    }

below are screen shots from wp admin and results. Mine is second one

Screen shot from WP admin and the result

+1  A: 

Check out wp_list_pages(). I think these settings will give you what you want.

<?php
$args = array(
    'child_of'     => $post->ID, // current page
    'depth'        => 1, // get children only, not grandchildren
    'echo'         => 1, // print immediately when we call wp_list_pages
    'sort_column'  => 'menu_order', // sort by the menu_order parameter
);      
?>
<?php wp_list_pages( $args ); ?>
artlung
@artlung: it gave me the same result as my code
Radek
So your code works too, just needed to use `global $post;`
Radek
@artlung: can I get the function return array of post ID only? not to print the list of post id or post names?
Radek
Radek, not with that function. There's probably a way to do it.
artlung
thank you. So it looks that I have to use get_pages function. It seems to me that wp_list_pages has easier to use. I like the depth arugument.
Radek
+1  A: 

try adding

global $post;

right before you declare $pages.

choise
@choise: you're genius :-) it did the trick. Could you tell me why it is needed there?
Radek
"the loop" sets up this global variable while looping through this posts. now you can access all objects out of the loop with this variable. (global is for scoping)
choise
I'm sorry...what `loop` are you talking about?
Radek
http://codex.wordpress.org/The_Loop <- the loop :)
choise
so if I want to use any variable from 'the loop' in my code I have to always use the magic word `global`?
Radek
if you want to use some content of the variable "post" (very much information stored in it) outside the loop, you first have to set it global
choise
gooooood, now I understood. Thank you for that
Radek