Skip to content

Instantly share code, notes, and snippets.

@twentyfortysix
Last active August 29, 2015 14:08
Show Gist options
  • Save twentyfortysix/07b23e5bd23b160c8a30 to your computer and use it in GitHub Desktop.
Save twentyfortysix/07b23e5bd23b160c8a30 to your computer and use it in GitHub Desktop.
WP function
// picture ids: array(1, 2, 3)
// $queries = array(
// array('large_screens','min-width: 1450px'),
// array('medium','max-width: 480px'),
// array('large','')
// );
// $class = class
function responsive_picture2($id, $class, $queries){
$with_euery = '';
$without_euery = '';
// if the query has no media query resolution set, print it out as it is
foreach($queries as $key => $query){
$att = wp_get_attachment_image_src($id , $query[0]);
if(!empty($query[1])){
$with_euery .= '<source srcset="'.$att[0].'" media="('.$query[1].')">';
}else{
$without_euery .= '<img srcset="'.$att[0].'" class="'.$class.'" alt="hohostyle '.$id.'" />';
}
}
if(!empty($with_euery) || !empty($without_euery)){
$return = '<picture>'.$with_euery."\n".$without_euery."\n".'</picture>';
}else{
$return = '';
}
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment