Snippets: Display “Secondary” Image For Blog Entry Thumbnail

The Total theme includes a "Secondary Thumbnail" field which is used for the "Image Swap" overlay style when using custom Visual Composer grids. However, you can make use of this field for anything you want! For example you can override the default featured image on your entries like the example below:

// Trick WordPress into thinking the post has a featured image.
// NOTE: only needed if you are NOT using the featured image as well
add_filter( 'has_post_thumbnail', function( $has_thumbnail ) {
	if ( get_post_meta( get_the_ID(), 'wpex_secondary_thumbnail', true ) ) {
		$has_thumbnail = true;
	}
	return $has_thumbnail;
}, 10 );

// Override the featured image ID
add_action( 'wpex_blog_entry_thumbnail_args', function( $args )  {

	// Alter thumbnail ID to display secondary image
	if ( $secondary = get_post_meta( get_the_ID(), 'wpex_secondary_thumbnail', true ) ) {
		$args['attachment'] = $secondary;
	}

	// return args
	return $args;
} );

Result:

Snippet Type: PHP

Posted Under: Blog

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