2

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="<?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
  • why not just call get_pages() to list child pages for each parent, then merge the returned arrays into $projectpage? Commented Dec 18, 2010 at 13:18

1 Answer 1

2

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="<?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="<?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...

8
  • There's probably room for improvement in the code, but it should still serve as a decent enough example...(i hope).. :)
    – t31os
    Commented Dec 18, 2010 at 14:48
  • Are array keys numerical, seem to be? array_merge() should handle such merge fine.
    – Rarst
    Commented Dec 18, 2010 at 15:07
  • The keys returned from get_pages are numerical yes, array_merge won't work here, it will see matching keys and only preserve the result from the left array(hence my loops)... (try it, you'll see what i mean)..
    – t31os
    Commented Dec 18, 2010 at 15:10
  • @t31os please see array_merge() documentation I linked to above, numerical keys are special case and are merged correctly. Try it yourself. :)
    – Rarst
    Commented Dec 18, 2010 at 17:35
  • Indeed, my mistake, i'll udpate the example..
    – t31os
    Commented Dec 18, 2010 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.