0

I am creating a template that would display children pages of a parent page.

Employees

♦ Managers

♦ IT guys

♦ Designers

I want parent page (employees) to display its children pages.

I've tried this: (in functions.php)

function wpb_list_child_pages() { 

global $post; 

if ( is_page() && $post->post_parent )

    $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
else
    $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );

if ( $childpages ) {

    $string = '<ul>' . $childpages . '</ul>';
}

return $string;

}

add_shortcode('wpb_childpages', 'wpb_list_child_pages');

and declaration in my page's template:

echo wpb_list_child_pages();

I get a list of children pages but as a list (of course, there is nothing wrong with the code)

I want to enlist children as items of an array so that I can do something like this:

$page1 = childrenID[0];
$page2 = childrenID[1];
$page3 = childrenID[2];

I want this so that I can theme children pages (by ID) on their parent page.

I hope I am clear.

Update

After using this: `global $post; $parent_id = the_ID();

$children = get_posts(array( 'post_parent' => $parent_id, 'fields' => 'ids' ));

echo $string = '

  • ' . implode('
  • ', $children) . '
  • ';`

    I am getting a list of IDs which are of some posts and not pages.

    1 Answer 1

    0

    You should use get_posts function.

    global $post;
    $parent_id = $post->ID;
    
    $children = get_posts(array(
        'post_parent' => $parent_id,
        'fields' => 'ids'
    ));
    
    $string = '<ul><li>' . implode('</li><li>', $children) . '</li></ul>';
    
    5
    • I used the_ID() function instead of $post->ID; in line 2. Is that Okay? Aug 8, 2016 at 12:54
    • the_ID() prints id, use get_the_ID() instead Aug 8, 2016 at 12:56
    • get_the_ID() shows blank list $string when I print it. Aug 8, 2016 at 13:11
    • the_ID() and get_the_ID() will only works inside loop. Otherwise you have to call global $post or get it from query vars to get post id Aug 8, 2016 at 13:21
    • i used global $post. Also I'm getting posts as children and not pages. Aug 8, 2016 at 13:25

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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