• Hello

    I’m trying to build a loop that displays the Parent title and then the title and content of any child pages. I’ve got as far as displaying the parent page titles, but I’m not stuck as to how to pull in the child title and content and where to place the code.

    Any help very much appreciated, thanks.

    <?php
    	$parent_pages = get_pages( array( 'parent' => 0, 'post_type' => 'archive' ) );
    
    foreach( $parent_pages as $parent_pages) {
    ?>
    
    	<h2><?php echo $parent_pages->post_title; ?></h2>
    
    	<?php
    	{ ?>
    
    <?php } ?>
    
    <?php
    
    }
      ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter leanda

    (@leanda)

    Update: Managed to get a little bit further, almost there I think. I can now see just one sub post (the latest) and the same sub post is duplicated under each parent title either though it isn’t a child of the others.

    Can anyone please help me nail this last bit. Thanks.

    <?php $parent_pages = get_pages( array(
                'parent' => 0,
                'post_type'=> 'archive'
                ));
    
            foreach( $parent_pages as $parent_pages)
                { ?>
    
            <h1><?php echo $parent_pages->post_title; ?></h1>
    
            <?php
            $children = get_pages(array(
                'orderby' => 'menu_order',
                'order' => 'ASC',
                'post_parent' => $post->ID,
                'post_type' => get_post_type( $post->ID )
                ));
    
            foreach($children as $child);
            ?>
            <h2><?php the_title(); ?></h2>
            <?php the_content(); ?>
    
            <?php } ?>

    You’re getting duplicate content because you need to be within the loop to use the_title() and the_content(). You could instead use your foreach loop to build an array of IDs and then use that array to build a secondary loop using post__in.

    Thread Starter leanda

    (@leanda)

    Thanks! Don’t think an array of IDs will work in this case because new pages and sub pages will be added and it needs to be dynamic.

    Any chance you could please elaborate on where to add the loop?

    Cheers

    You could build an array iteratively using foreach() once you have an array of posts from get_pages(). Something like this:

    $child_array = array();
    foreach ( $children as $child ) {
        $child_array[] .= $child->ID;
    }

    That gets you $child_array, an array of the IDs that are a child page of the page in question. From there, you make a new WP_Query object and pass that array as an argument to post__in. Something like this:

    <?php
    $child_query = new WP_Query( array( 'post__in' => $child_array, 'post_type' => 'page' ) );
    if ( $child_query->have_posts() ) : while ( $child_query->have_posts() ) : $child_query->the_post() {
    ?>
        /*  you can use the_title(), the_content(), etc., normally here */
    <?php endwhile; endif; // end loop ?>
    Thread Starter leanda

    (@leanda)

    Thanks, I’ll give this a whirl.

    Thanks again for your help.

    kosimoff

    (@kosimoff)

    Hello leanda and stephencottontail,
    I dont know much php, could you please give me the full code for this thread?

    I want current page Title, and children page Title and children page content.

    thank you

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Display Parent Title and Child Page Content’ is closed to new replies.