Show children of child page within a query
-
I am trying to find a way to show a first level page’s children’s title, thumbnail and a few custom fields. I have been able to successfully show the children of the first level (send level). Where I am getting stuck though is that within each child page I’d like to show it’s children as well (third level). I fist tried setting this up using $wpdb->get_results however it seems like after looking at other support forums it looks like I should be using WP_Query instead.
If anyone can help point me in the right direction it would be greatly appreciated. Below are the two options I’ve tried.
<?php // The Query $the_query = new WP_Query( array( 'post_type' => 'page', 'post_parent' => $post->ID, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC' ) ); // The Loop if ( $the_query->have_posts() ) while ( $the_query->have_posts() ) : $the_query->the_post(); $thumbnail = get_the_post_thumbnail('sample-thumbnails'); $postexcerpt = get_the_excerpt(); ?> <div class="sub-container group"> <div class="thumbnail-wrap t-1of2 d-1of2"><?php the_post_thumbnail('thumbnail') ?></div> <div class="sub-content t-1of2 d-1of2 last-col"><?php the_title(); ?> <p><?= $postexcerpt ?></p> <!--list 3rd level pages --> <?php $granchildren = get_page_children($page->ID, $pages); ?> <?php foreach ($granchildren as $grandchild): ?> <h3><?php echo $grandchild->post_title; ?></h3> <?php endforeach; ?> </div> </div> <?php endwhile; ?>
<? $child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' AND post_status = 'publish' ORDER BY menu_order", 'OBJECT'); if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); $thumbnail = get_the_post_thumbnail($pageChild->ID, 'sample-thumbnails'); $postexcerpt = get_the_excerpt($pageChild->ID); ?> <div class="sub-container group"> <div class="thumbnail-wrap t-1of2 d-1of2"> <?php if( $caption ): ?> <img src="<?php echo $thumbnail['url']; ?>" alt="<?php echo $thumbnail['alt']; ?>" /> <?php else:?> <img src="<?php bloginfo('template_url'); ?>/images/default.png" width="490" height="345" alt="Image to come"/> <? endif;?> </div> <div class="sub-content t-1of2 d-1of2 last-col"> <h1><a href="<?= get_permalink($pageChild->ID) ?>" rel="bookmark" title="<?= $pageChild->post_title ?>"><?= $pageChild->post_title ?></a></h1> <p><?= $postexcerpt ?></p> <!--list 3rd level pages --> <?php $granchildren = get_page_children($pageChild->ID, $pages); ?> <?php foreach ($granchildren as $grandchild): ?> <h3><?php echo $grandchild->post_title; ?></h3> <?php endforeach; ?> </div> </div> <? endforeach; endif;?>
- The topic ‘Show children of child page within a query’ is closed to new replies.