Tom J Nowell
Tom J Nowell

Reputation: 9981

Wordpress retrieving Post children

I have a page template where I want to list entries for each child page ( but not their child pages), and display some things based on the pages name, and custom fields. How would I do this?

Using wordpress 2.8

Upvotes: 0

Views: 2310

Answers (2)

Tom J Nowell
Tom J Nowell

Reputation: 9981

Here's how I did it in the end in my page template using get_children:

$pages = get_children( [
    'post_type'   => 'page',
    'orderby'     => 'title',
    'order'       => 'ASC',
    'post_parent' => get_the_ID(),
] );

if ( empty( $pages ) ) {
    // no attachments here
} else {
    foreach ( $pages as $post_id => $_post ) {
        ?>
        <h3><?php echo esc_html( get_the_title( $post_id ) ); ?></h3>
        <p><a href="<?php echo esc_url( get_permalink( $post_id ) ); ?>">View more</a></p>
        <?php
    }
}

get_children uses WP_Query internally but with extra quirks, so if we go straight to the source we get:

$q = new WP_Query( [
    'post_type'   => 'page',
    'orderby'     => 'title',
    'order'       => 'ASC',
    'post_parent' => get_the_ID(),
] );

if ( ! $q->have_posts() ) {
    // no attachments here
} else {
    while( $q->have_posts() ) {
        $q->the_post();
        ?>
        <h3><?php the_title(); ?></h3>
        <p><a href="<?php echo esc_url( get_permalink() ); ?>">View more</a></p>
        <?
    }
    wp_reset_postdata();
}

Upvotes: 4

Brock Boland
Brock Boland

Reputation: 16690

I'm doing something similar in my site. You need to define your own page template. Create a file in your template directory and paste this in there - to start with, anyway. Then edit your parent page, and select Parent Page for Template in the Attributes box.

This example will include the full contents of each child page, but you can customize however you want to.

<?php
/*
Template Name: Parent Page
*/
?>
<?php
/**
 * Loop over all sub-pages and include their content
 */
the_post();
$children = get_pages("child_of=" . $post->ID);
$childIDs = array();
foreach($children as $c) {
    $childIDs[] = (int)$c->ID;
}

query_posts(array('post_type'=>'page','post__in'=>$childIDs, 'orderby'=>'menu_order'));

get_header();
?>
<div id="content">
    <?php if (have_posts()) : while (have_posts()) : the_post(); $loopcounter++; ?>

        <div <?php if (function_exists('post_class')) post_class(); ?>>

            <div class="entry entry-<?php echo $postCount ;?>">

                <div class="entrytitle_wrap">
                    <?php if (!is_page()) : ?>
                        <div class="entrydate">
                            <div class="dateMonth">
                                <?php the_time('M');?>
                            </div>
                            <div class="dateDay">
                                <?php the_time('j'); ?>
                            </div>
                        </div>
                    <?php endif; ?>

                    <div class="entrytitle">
                    <?php if ($loopcounter==1):?>  
                        <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Link to <?php the_title(); ?>"><?php the_title(); ?></a></h1> 
                    <?php else : ?>
                        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2> 
                    <?php endif; ?>
                    </div>

                </div>


                <div class="entrybody"> 
                    <?php if (is_archive() || is_search()) : ?> 
                        <?php the_excerpt(); _e('<p><a href="'.get_permalink().'">Continue reading about '); the_title(); _e('</a></p>');  ?>
                    <?php else : ?>
                        <?php the_content('Read the rest of this entry &raquo;');   ?>
                        <?php the_tags( '<p>Tags: ', ', ', '</p>'); ?>
                    <?php endif; ?>         
                </div>

                <div class="entrymeta"> 
                    <div class="postinfo"> 
                        <?php edit_post_link('Edit', '', ''); ?>                
                    </div>  
                </div>


                <?php if ($loopcounter == 1 && !is_singular()) { include (TEMPLATEPATH . '/ad_middle.php'); } ?>                 

            </div>  

    </div>

    <?php endwhile; ?>

    <?php if (!is_singular()): ?>         
        <div id="nav-global" class="navigation">
            <div class="nav-previous">
            <?php 
                next_posts_link('&laquo; Previous entries');
                echo '&nbsp;';
                previous_posts_link('Next entries &raquo;');
            ?>
            </div>
        </div>

    <?php endif; ?>

    <?php else : ?>

        <h2>Not Found</h2>
        <div class="entrybody">Sorry, but you are looking for something that isn't here.</div>
    <?php endif; ?>

</div>

<?php get_footer(); ?>

Upvotes: 0

Related Questions