Skip to content

Snippet: Custom Post Gallery Loop

The Total theme includes the ability to assign images to your post via the Gallery Metabox (available for core theme post types but can be extended for other post types). If you want to create a custom module, shortcode or template file you can easily grab an array of the images attached to the post and display them on the front-end. Here is a useful example:

// Check if post has gallery images if so do stuff
if ( $images = wpex_get_gallery_ids() ) {
	
	// Loop through image ids
	foreach ( $images as $image_id ) :

		// Generate thumbnail
		$thumbnail =  wpex_get_post_thumbnail(
			'attachment'    => $image_id,       // ID of image
			'size'          => 'full',          // Registered size
			'width'         => '',              // Custom width
			'height'        => '',              // Custom height
			'crop'          => 'center-center', // Custom crop
			'alt'           => '',              // Custom Alt
			'class'         => '',              // Custom Classes
		);

		// Echo thumbnail
		if ( $thumbnail ) {
			echo $thumbnail;
		}

	// End loop
	endforeach;

}
All PHP snippets should be added via child theme's functions.php file or via a plugin. We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top