Child page from 2 different parent pages

currently I have 3 parent page types with multiple child pages under each.

Feature
* feat 1
* feat 2
..

Creative
* creat 1
* creat 2

Info
* info 1
* info 2

I have been successful with pulling the children page content onto the parent page, but now I want to get the children of Feature and Creative onto Creative Parent page.

here is what I am using to get the child pages of Featured parent page.

<div id="folio-menu">
<div class="inner">
<?php
$projectpage = get_pages('child_of=14&sort_column=post_date&sort_order=desc');

$count = 0;
foreach($projectpage as $page)
{
$content = $page->post_content;
if(!$content)

continue;
if ($count == -1)
break;
$count++;

$content = apply_filters('the_content', $content);

?>
<div class="thumb-container">
<div class="thumb"><a href="https://wordpress.stackexchange.com/questions/5580/<?php echo get_permalink($page->ID); ?>"> <?php echo get_image ("thumbnail",1,1,1,$page->ID);?></a>
</div><p class="smalltitle"><?php echo $page->post_title ?></p>
</div>
<?php
}
?>
</div>

I tried to list both parent page id’s in ‘child_of=14,15&…’ but it did not work.

Thanks!

1 Answer
1

Like AmbitiousAmoeba mentioned, you’ll need to perform a few get_pages calls to lookup the pages for each child, merge the results together, and iterate over those results.

I worked up an example for you, because it’s fiddly trying to merge arrays when they have matching indexes, the only thing you should need modify are the child_of parameters, and of course add back in your surrounding HTML..

Updated example

Uses array_merge instead, i mistaken thought it wouldn’t work for with numeric keys(it does though, as Rarst kindly pointed out in the comments)..

$args = array( 
     'child_of'    => 174,
     'sort_column' => 'post_date',
     'sort_order'  => 'desc'
);
$child_pages = get_pages( $args );

$args['child_of'] = 143;
$second_page_group = get_pages( $args );
$child_pages = array_merge( $child_pages, $second_page_group );

$args['child_of'] = 146;
$third_page_group = get_pages( $args );
$child_pages = array_merge( $child_pages, $third_page_group ); 

unset( $second_page_group, $third_page_group, $args );

if( !empty( $child_pages ) )
    foreach( $child_pages as $_my_page ) {
        $title   = apply_filters( 'the_title', $_my_page->post_title );
        //$content = apply_filters( 'the_content', $_my_page->post_content );
        //$content = str_replace( ']]>', ']]&gt;', $content );
        $link    = apply_filters( 'the_permalink', get_permalink( $_my_page->ID ) );
        ?>
        <div class="thumb-container">
            <div class="thumb"><a href="https://wordpress.stackexchange.com/questions/5580/<?php echo $link ?>"> <?php //echo get_image( "thumbnail", 1, 1, 1, $_my_page->ID );?></a></div>
            <p class="smalltitle"><?php echo $title ?></p>
        </div>
        <?php
    }

Old example

Might aswell leave the old example in place

// Setup base args for each get_pages call
$args = array( 
    'child_of'    => 174,
    'sort_column' => 'post_date',
    'sort_order'  => 'desc'
);
// Perform the first query to get pages
$child_pages = get_pages( $args );
// Count the results
$c = count( $child_pages );
// Update the child_of arg to another ID
$args['child_of'] = 143;
// Perform the second call to get children pages
$second_page_group = get_pages( $args );

// If there were pages in the first group
if( $c )
    // Add them into the first array returned
    foreach( $second_page_group as $k => $p ) {
        $child_pages[$c] = $p;
        $c++;
    }
else
    // Else fill the array with the results of the second query
    $child_pages = $second_page_group;
// Cleanup
unset($second_page_group);
// Count the results again
$c = count( $child_pages );
// Update the child_of arg to another ID before the final get_pages call
$args['child_of'] = 146;
// Make the third call
$third_page_group = get_pages( $args );
// If we have results, add these into that array
if( $c )
    foreach( $third_page_group as $k => $p ) {
        $child_pages[$c] = $p;
        $c++;
    }
else
    // Else fill the array with the results of the final query
    $child_pages = $third_page_group;
// Cleanup
unset($third_page_group);

if( !empty( $child_pages ) )
    foreach( $child_pages as $_my_page ) {
        $title   = apply_filters( 'the_title', $_my_page->post_title );
        //$content = apply_filters( 'the_content', $_my_page->post_content );
        //$content = str_replace( ']]>', ']]&gt;', $content );
        $link    = apply_filters( 'the_permalink', get_permalink( $_my_page->ID ) );
        ?>
        <div class="thumb-container">
            <div class="thumb"><a href="https://wordpress.stackexchange.com/questions/5580/<?php echo $link ?>"> <?php echo get_image( "thumbnail", 1, 1, 1, $_my_page->ID );?></a></div>
            <p class="smalltitle"><?php echo $title ?></p>
        </div>
        <?php
    }

Hope that helps…

Leave a Comment