Currently I'm trying to make a dynamic bootstrap collapse with child page information. The problems I'm facing is how to get the_content(); (it's all text without a read more option) of a child page and how can I make div ID's unique?

<div class="row">
<div id="accordion" class="panel-group">

$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="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title"><a class="accordion-toggle" href="#UNIQUEID" data-toggle="collapse" data-parent="#accordion">
<?php echo $pageChild->post_title; ?>
<i class="indicator glyphicon glyphicon-chevron-down pull-right"></i>
<div id="#UNIQUEID" class="panel-collapse collapse">
<div class="panel-body"><?php echo $pageChild->post_content; ?></div>

<?php endforeach; endif;


Can anyone help me out?

Thanks in advance!

1 Answer 1


You rarely ever need a custom SQL query, use the tools WordPress has to offer:

$query = new WP_Query(
        'posts_per_page' => -1,
        'post_parent' => $post->ID,
        'post_type' => 'page',
        'orderby' => 'menu_order post_title',
        'order' => 'ASC',

if ( $query->have_posts() ) : ?>

    <?php while ( $query->have_posts() ) : $query->the_post() ?>

        <div id="page-<?php the_ID() ?>">
            <?php the_content() // Use any template tags as you would normally ?>       

    <?php endwhile ?>

    <?php wp_reset_postdata() // Restore the current page ?>

<?php endif ?>
  • Thanks a lot! This is the correct answer. I'll post the total code under this answer.
    – rwzdoorn
    Mar 11, 2015 at 13:27
  • @rwzdoorn I'd say don't worry - this answer sufficiently explains the concept, your exact code is unlikely to be relevant for anyone else. Mar 11, 2015 at 13:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.