• Hi.

    I need to get the pages of a custom post type exactly as they are in the admin panel.
    Example:

    Page1
    –subpage1
    –subpage2
    Page2
    Page3
    –subpage1
    –subpage2

    I need this as a loop.

    I tried for a few days to do this but no success.

    Can anyone help me? Thank you. Nelu?u.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi NelutuO.

    Do you have any code to show?

    Thread Starter NelutuO

    (@nelutuo)

    I’m a beginner so what I have is very basic and may not help to much.
    I have a basic loop and two variables that I thought may hold the key.

    <?php $loop= new WP_Query( array( 'post_type'=> 'post-type', 'posts_per_page' => -1 ) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <?php echo $id = get_the_ID(); ?>
         -
        <?php echo $parent = $post->post_parent; ?>
    
        <?php the_title(); echo "<br />"; ?>
    
    <?php endwhile; wp_reset_query(); ?>

    What I was trying to do but didn’t managed is this:

    display the pages that have the parent equal to zero
    check if the page is a parent
    if it is -> loop and display the child pages

    I don’t know if WP_Query can display the pages that have a specific parent (wich we know since we have its id) and I don’t know if a WP_Query can be used inside another WP_Query

    Use this:

    $loop = new WP_Query( array( 'post_type' => 'your_post_type', 'post_parent' => 0 , 'posts_per_page' => -1 ) );
    while ( $loop->have_posts() ) : $loop->the_post();
        //parent page title
        the_title();
        $parent_page_id = $post->ID;
        $child_pages = get_pages( array( 'child_of' => $parent_page_id , 'post_type' => 'your_post_type', 'posts_per_page' => -1 ) );
        if ( $child_pages != false ) {
            foreach( $child_pages as $page ) {
                 //child page title
                echo  $page->post_title;
            }
         }
    endwhile;

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Post type tree loop’ is closed to new replies.