Question

Child of child ?

So this is the code I am using to get child pages as links and their thumbnail ( featured image ) :

$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT');

    if ( $child_pages ) :
        foreach ( $child_pages as $pageChild ) :
            setup_postdata( $pageChild );
            $thumbnail = get_the_post_thumbnail($pageChild->ID, 'thumbnail', true);
            if($thumbnail == "") continue; // Skip pages without a thumbnail
            $thumbShort = explode('src="', $thumbnail);
            $thumbnailShortStripped = substr($thumbShort[1], 0, strpos( $thumbShort[1], '"'));
    ?>
            <li><a href="<?= get_permalink($pageChild->ID) ?>" rel="bookmark"><img src="<?= $thumbnailShortStripped; ?>" class="thumbnail" alt="<?= $pageChild->post_title ?>" 0="" /></a></li>
    <?
    endforeach;
    endif;
    ?>

But how could I get the child of child?

So I just want the " grandchild ", how could I do it?

Was it helpful?

Solution

Thanks all, I figured it out my self ! :)

 <?
$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT');
$i = 0;
if ( $child_pages ) :
foreach ( $child_pages as $pageChilds ) :
    $gchild_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$pageChilds->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT');
    if($gchild_pages) :
    foreach ($gchild_pages as $pageChild):
    setup_postdata( $pageChild );
    $thumbnail = get_the_post_thumbnail($pageChild->ID, 'thumbnail', true);
    if($thumbnail == "") continue; // Skip pages without a thumbnail

?>
    <a href="<?= get_permalink($pageChild->ID) ?>" rel="bookmark">
       <div class="child-thumb">
      <li> <?= $thumbnail ?></li>
       <div class="child-text"><?= $pageChild->post_title ?></div>
       </div>
    </a>
<?

    endforeach;
    endif;
endforeach;
endif;
?>

Thanks all!

OTHER TIPS

you can use below function

$args = array(   
   'child_of' => $parentpostID,
   'post_status' => 'publish',
 );

$data = get_pages( $args );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top