I would like to get the URL's of my custom image sizes for a featured image - so I can inject them conditionally.
(this in functions.php)
To start off, I'm removing the default crop sizes because I never use them.
// remove all the default image sizes
function sgr_filter_image_sizes( $sizes) {
unset( $sizes['thumbnail']);
unset( $sizes['medium']);
unset( $sizes['large']);
return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'sgr_filter_image_sizes');
Then defining the new sizes that I'll use.
// add custom image sizes
function add_custom_image_sizes() {
add_image_size ( 'small-screen', 700, 350, true );
add_image_size ( 'medium-screen', 1200, 600, true );
add_image_size ( 'large-screen', 2000, 1000, true );
add_image_size ( 'extra-large-screen', 3200, 1600, true );
}
add_action('init', 'add_custom_image_sizes');
Then in my HTML/PHP, I'm doing something like this
<?php if ( has_post_thumbnail() ) { ?>
<?php $thisPostId = get_post_thumbnail_id(); ?>
<img src="some-blank-image-of-the-correct-ratio.png"
data-url-small="<?php echo wp_get_attachment_image_src( $thisPostId, 'small-screen' ); ?>"
data-url-medium="<?php echo wp_get_attachment_image_src( $thisPostId, 'medium-screen' ); ?>"
data-url-large="<?php echo wp_get_attachment_image_src( $thisPostId, 'large-screen' ); ?>"
data-url-extra-large="<?php echo wp_get_attachment_image_src( $thisPostId, 'extra-large-screen' ); ?>"
alt=''>
<?php } else { ?>
<img src='http://placehold.it/1600x800/333/&text=No featured image yet' alt=''>
<?php } ?>
Then in my JavaScript - I can choose white URL to use based on a set of circumstances.
I'm getting a soft failure that returns the full image for each.
This is on a single-customPostType page, and is not in a loop.
wp_get_attachment_image_src
.