Skip to content

Instantly share code, notes, and snippets.

@fedek6
Created September 28, 2015 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedek6/93fb2af92f3f708ec195 to your computer and use it in GitHub Desktop.
Save fedek6/93fb2af92f3f708ec195 to your computer and use it in GitHub Desktop.
Generate data tags for http://jquerypicture.com/ usable in Wordpress
<?php
/**
* Responsive bg data
*
* @see http://jquerypicture.com/
*/
function generate_reponsive_bg() {
global $post, $_wp_additional_image_sizes;
// smallest one from 0 to 768
$img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'sm');
echo 'data-media="' . $img[0] . '" ';
unset( $img );
/** @var array $sizes id of additional sizes **/
$sizes = array_keys( $_wp_additional_image_sizes );
$sizes[] = 'full';
// remove sm (it's already printed)
array_shift( $sizes );
/** @var array $widths **/
$widths = array_column( $_wp_additional_image_sizes, 'width' );
// combine id with shifted values
$sizes = array_combine( $sizes, $widths );
// print data tags & FIN
foreach( $sizes as $id => $size ) {
$url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), $id );
echo 'data-media' . $size . '="' . $url[0] . '" ';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment