What if you need to display all thumbnails of all pages under the frequently asked questions parent page on the homepage or another page? I initially thought a [WP_Query][1] will be the best way to do this.

To load data from a parent page with slug x I could use parameter:

$query = new WP_Query( 'name=faq' );

I wanted to load all featured images from all pages under parent page faq though. On reading WordPress Support thread [here][2] I thought about refining this snippet:

<?php $subs = new WP_Query( array( 'post_parent' => $post->ID, 'post_type' => 'page', 'meta_key' => '_thumbnail_id' ));
 if( $subs->have_posts() ) : while( $subs->have_posts() ) : $subs->the_post();
 echo get_the_post_thumbnail($post->ID);
 endwhile; endif; wp_reset_postdata(); ?>

Then I found [this thread][3] at WordPress SO. It does seem to be better and uses [get_pages][4] instead of WP_Query. Somewhat adjusted code:

$args = array(
 'post_type' => 'page', // type of post
 'child_of' => 6, // my faq parent page ID
 );
 
 $pages = get_pages( $args );
 foreach( $pages as $page ) { // run a foreach loop
 if( has_post_thumbnail( $page->ID ) {
 $imgdata = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
 $imgwidth = $imgdata[1]; // thumbnail's width
 $imgheight = $imgdata[2]; // thumbnail's height
 }
 }
 ?>
 <div class="child element isotope-item" style="width:<?php echo $imgwidth;?>;height:<?php echo $imgheight;?>;">
 <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
 <?php echo the_post_thumbnail(large); ?>
 <span class="child_title"><?php echo the_title_attribute();?></span>
 </a>
 </div>
 <?php
 endif;
 // end your stuff.
 endwhile;
 else :
 // Do the no posts found message 
 endif;
 ?>
 </div>

But that would not have been quite enough for the display the client wanted as they wanted to load a slider after four thumbnails and then continue with the display of the thumbnails.  That information will be added a little later on.
[1]: http://codex.wordpress.org/Class_Reference/WP_Query
[2]: https://wordpress.org/support/topic/display-child-pages-featured-images-in-a-parent-page?replies=16
[3]: http://wordpress.stackexchange.com/questions/106591/displaying-all-parent-pages-as-featured-images
[4]: http://codex.wordpress.org/Function_Reference/get_pages

featured image: https://flic.kr/p/9dXKU2

Leave a Reply

Your email address will not be published. Required fields are marked *