Skip to content

Instantly share code, notes, and snippets.

@fedek6

fedek6/.php

Created Sep 28, 2015
Embed
What would you like to do?
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