-
-
Save fedek6/93fb2af92f3f708ec195 to your computer and use it in GitHub Desktop.
Generate data tags for http://jquerypicture.com/ usable in Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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