Skip to content

Instantly share code, notes, and snippets.

@nickdavis
Last active August 16, 2016 08:09
Show Gist options
  • Save nickdavis/2b07d4e1bc639b4a8c06cc2757458422 to your computer and use it in GitHub Desktop.
Save nickdavis/2b07d4e1bc639b4a8c06cc2757458422 to your computer and use it in GitHub Desktop.
Check the width of a post's featured image in WordPress and use it to determine which image size should be displayed
<?php
function nd_featured_image_size_sniff() {
if ( ! has_post_thumbnail() ) {
return;
}
$featured_image_id = get_post_thumbnail_id( get_the_ID() );
$featured_image_metadata = wp_get_attachment_metadata( $featured_image_id );
if ( $featured_image_metadata['width'] > 819 ) {
// Display large image size
} else {
// Display fallback smaller image size
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment