TechyDude
TechyDude

Reputation: 1165

Wordpress: Query child pages content while on parent pages

I have custom post types setup - called projects and enabled child pages. While I am on the parent page, I want to query the content of the child pages on the parent page.

Here is my attempt. This queries ALL custom post types called projects. Can anyone help me query the child pages?

<?php

$args = array(

    'child_of' => $post->ID, 
    'post_type' => 'projects', 

    'order'          => 'ASC',
    'orderby'        => 'menu_order'
 );


$wpq = new WP_Query( $args );
if( $wpq->have_posts() ): while( $wpq->have_posts() ): $wpq->the_post(); ?>

        <div id="parent-<?php the_ID(); ?>" class="parent-page">
            <?php echo the_content();?>
        </div>

    <?php endwhile; ?>

<?php endif; wp_reset_query(); ?>

Upvotes: 1

Views: 10304

Answers (2)

Purva Bathe
Purva Bathe

Reputation: 11

<?php

$query = array('post_parent' => $post->ID, 'post_type' => 'page', 'posts_per_page' => '3','orderby' => 'date','order' => 'DESC');


                $loop = new WP_Query($query);

            if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();
            ?>
                    <div class="section-container">
                        <?php echo wp_trim_words( get_the_content(), 40, '...' ); ?>
                    </div>


            <?php endwhile; endif; ?>

Upvotes: 0

TechyDude
TechyDude

Reputation: 1165

I figured it out. Hopefully anyone else having this question will find this! I found the wordpress codex for get_pages: http://codex.wordpress.org/Function_Reference/get_pages

<?php $children = get_pages( 
    array(
        'sort_column' => 'menu_order',
        'sort_order' => 'ASC',
        'hierarchical' => 0,
        'parent' => $post->ID,
        'post_type' => 'projects',
    ));

foreach( $children as $post ) { 
        setup_postdata( $post ); ?>
    <div class="section-container">
        <?php the_content(); ?>
    </div>
<?php } ?>

Upvotes: 7

Related Questions