Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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();