Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
I used this code to loop through child pages of a custom post type called work. Each Work piece had a title, featured image and pdf. The child pages, describe the details of the project or work piece and display as an accordion. So basically what we have is an accordion within an accordion.
<?php
// loop through the sub-pages of your custom post type
$childpages = new WP_Query( array(
'post_type' => 'work',
'post_parent' => $this_page,
'posts_per_page' => 100,
'orderby' => 'menu_order'
));
while ( $childpages->have_posts() ) : $childpages->the_post(); ?>
<section>
<h2 class="title"><a href="#"><?php the_title(); ?></a></h2>
<div class="content">
<?php the_content();?>
<?php $this_subpage=$post->ID; ?>
<?php
//Loop through the sub-pages of the child pages next
$subpages = new WP_Query( array(
'post_type' => 'work',
'post_parent' => $this_subpage,
'posts_per_page' => -1,
'orderby' => 'menu_order'
));
while ( $subpages->have_posts() ) : $subpages->the_post(); ?>
<section class="subpages">
<h3><a href="#"><?php the_title(); ?></a></h3>
<div>
<?php the_content();?>
</div>
</section>
<?php endwhile; wp_reset_query(); ?>
</div><!--.content -->
</section>
<?php endwhile; wp_reset_query(); ?>
@jacquesletesson

This comment has been minimized.

Copy link

@jacquesletesson jacquesletesson commented Jan 24, 2015

Thanks for sharing.

On line 42 you should replace wp_reset_query() with wp_reset_postdata()

More information here : https://wordpress.stackexchange.com/questions/144343/wp-reset-postdata-or-wp-reset-query-after-a-custom-loop

@mikeritter

This comment has been minimized.

Copy link

@mikeritter mikeritter commented Oct 20, 2015

Where do you define $this_page?

@danielfonda

This comment has been minimized.

Copy link

@danielfonda danielfonda commented Jul 30, 2016

You sir, are awesome and I love you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment