Skip to content

Instantly share code, notes, and snippets.

@wiesson
Last active August 29, 2015 14:17
Show Gist options
  • Save wiesson/eae2b4542864b2d14a51 to your computer and use it in GitHub Desktop.
Save wiesson/eae2b4542864b2d14a51 to your computer and use it in GitHub Desktop.
Super simple responsive Wordpress gallery approach
function responsiveImages($string, $attr) {
$output = "<div id=\"responsive-gallery-test\">";
$posts = get_posts(array('include' => $attr['ids'], 'post_type' => 'attachment'));
foreach ($posts as $image) {
$output .= "<img data-img-small='" . wp_get_attachment_image_src($image->ID, 'thumbnail')[0] . "'" .
" data-img-medium='" . wp_get_attachment_image_src($image->ID, 'medium')[0] . "''" .
" data-img-large='" . wp_get_attachment_image_src($image->ID, 'large')[0] . "''" .
" data-img-full='" . wp_get_attachment_image_src($image->ID, 'full')[0] . "'/>";
}
$output .= "</div>";
return $output;
}
<div id="responsive-gallery-test">
<img
data-img-small="/wp-content/uploads/2015/03/A-100x47.jpg"
data-img-medium="/wp-content/uploads/2015/03/A-300x141.jpg"
data-img-large="/wp-content/uploads/2015/03/A-1024x481.jpg"
data-img-full="/wp-content/uploads/2015/03/A.jpg" />
<img
data-img-small="/wp-content/uploads/2015/03/B-100x47.jpg"
data-img-medium="/wp-content/uploads/2015/03/B-300x141.jpg"
data-img-large="/wp-content/uploads/2015/03/B-1024x481.jpg"
data-img-full="/wp-content/uploads/2015/03/B.jpg" />
</div>
setImageSrc($(window).width());
$(window).resize(function () {
setImageSrc($(window).width());
});
setImageSrc setImage(width) {
var img = null;
if (width < 767) {
img = 'data-img-small';
// console.log('screen-xs');
}
if (width >= 768) {
img = 'data-img-medium';
// console.log('screen-sm');
}
if (width >= 992) {
img = 'data-img-large';
// console.log('screen-md');
}
if (width >= 1200) {
img = 'data-img-full';
// console.log('screen-lg');
}
$('#responsive-gallery-test').find('img').each(function() {
this.setAttribute('src', this.getAttribute(img));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment