Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Theme Development Custom Post Type Templates in WordPress The Portfolio Homepage

Stefaan Quackels
Stefaan Quackels
5,720 Points

How to create an image gallery

Here they get featured images to be displayed from each indivual portfolie piece. But what if I want to create an image gallery? Advanced Custom Fields only has a paid option for this.

Is there not a way to filter out the images from a certain post_type using the WP-query? Can you anybody me any other suggestions or links to certain code snippets?

Thanks,

Stefaan

1 Answer

Stefaan Quackels
Stefaan Quackels
5,720 Points

Have actually managed to solve this using 2 WP-queries and an 'post_parent__in' argument. Here it is:

$query = new WP_Query( array( 'post_type' => 'realisatie-gallerij', 'posts_per_page' => -1, 'fields' => 'ids' ) ); $image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => -1, 'post_parent__in' => $query->posts, 'order' => 'DESC' ) );

if( $image_query->have_posts() ){ echo '<ul class="gallery">'; while( $image_query->have_posts() ) { $image_query->the_post(); $imgurl = wp_get_attachment_url( get_the_ID() );

  echo '<li class="gallery-columns-4 gallery-item" style="background-image: url('. $imgurl . ');"><a href="'.$imgurl.'"></a></li>';

} echo '</ul>'; } wp_reset_postdata();