• Hello, I’m wondering if someone clever can help with this problem.

    I have a custom post type which is hierarchical and has page attributes so I can create sub pages.

    I’m trying to display the title only of the parent page followed by the title and the content of it’s child pages. For example:

    Parent Page Title
    – Child Page Title
    – Child Page Content

    Parent Page Title
    – Child Page Title
    – Child Page Content

    and so on.

    The code below almost gets me there but it’s duplicating the child pages in the first bit as h1 titles. How can I exclude the child pages and prevent them from displaying in the first part of the loop?

    Maybe there is an easier way to approach this?

    Many thanks

    <?php
    
    echo "<ul>";
    if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        echo "<li><h1>".get_the_title()."</h1>";
    
        $args=array(
                'orderby' => 'menu_order',
                'order' => 'ASC',
                 'post_parent' => $post->ID,
                     'post_type' => get_post_type( $post->ID ),
                'posts_per_page' => 10
        );
    
        $childpages = new WP_Query($args);
    
        if($childpages->post_count > 0) { /* display the children content  */
            echo "<ul>";
            while ($childpages->have_posts()) {
                 $childpages->the_post();
                 echo "<li><h2>".get_the_title()."</h2></li>";
                 echo "<li><h2>".the_content()."</h2></li>";
            }
            echo "</ul>";
        }
        wp_reset_query();
    
        echo "</li>";
         }
        }
       echo "</ul>";
     ?>
  • The topic ‘Display Parent Page Title and Child Page Title and Content’ is closed to new replies.