Snippets: 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;

}

Snippet Type: PHP

Posted Under: Core

All PHP snippets should be added via a child theme's functions.php file.