List Child Pages

<?php $child_pages = $wpdb->get_results("SELECT *    FROM $wpdb->posts WHERE post_parent = " . $post->ID . "    AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?> <?php if ($child_pages):
    foreach ($child_pages as $pageChild):
        setup_postdata($pageChild); ?> <div class="child-thumb">   <?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?>  <a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a> </div> <?php
    endforeach;
endif; ?>

This can be used as part of a for loop to list child pages inside of widget items like so:

foreach($products_slider as $post) : setup_postdata($post); 						?> 							<div class="product_item"> 							 								<?php the_post_thumbnail(); ?> 								<div class="product_title"> 									<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> 									 									<ul class="subproducts"> 										<?php $child_pages = $wpdb->get_results("SELECT *    FROM $wpdb->posts WHERE post_parent = " . $post->ID . "    AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?> 										<?php if ($child_pages):
    foreach ($child_pages as $pageChild):
        setup_postdata($pageChild); ?> 										 										  <?php //echo get_the_post_thumbnail($pageChild->ID, 'thumbnail');
         ?> 										 <li><a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></li> 										 										<?php
    endforeach;
endif; ?> 									</ul> 									 								</div> 							 							</div> 						<?php
endforeach;
wp_reset_postdata();

Sourced here To list child pages or same level pages, when on a child page, use this example from WordPress:

<?php if ($post->post_parent) $children = wp_list_pages("title_li=&child_of=" . $post->post_parent . "&echo=0");
else $children = wp_list_pages("title_li=&child_of=" . $post->ID . "&echo=0");
if ($children)
{ ?>   <ul>   <?php echo $children; ?>   </ul>   <?php
} ?>

The above example will list all subpages inclusively, including same level pages. To create a submenu that excludes parent pages, and lists child items in a drill down method, use the following:

<?php global $post;
if (is_page())
{
    $page = $post->ID;
    if ($post->post_parent)
    {
        $page = $post->post_parent;
    }
    $theparent = $ancestors[0];
    $oneup = $ancestors[1];
    $lastlevel = $ancestors[2];
    $children = wp_list_pages('title_li=&echo=0&depth=1&child_of=' . $post->ID);
    $sub = wp_list_pages('title_li=&echo=0&depth=1&child_of=' . $post->post_parent);
    $twolevels = wp_list_pages('title_li=&echo=0&depth=0&child_of=' . $theparent);
    if ($lastlevel)
    {
        echo $twolevels;
    }
    elseif ($children)
    {
        echo $children;
    }
    elseif ($sub)
    {
        echo $sub;
    }
} ?>

Leave a Reply

Posted in php
katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.