Created
December 5, 2016 09:47
-
-
Save geeac/6237c9fba7dca63e3caa27cb717b3a7d to your computer and use it in GitHub Desktop.
change img srcset
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
/*function adjust_image_sizes_attr( $sizes, $size ) { | |
$sizes = '(max-width: 300px) 300px, (max-width: 768px) 768px, (max-width: 1024px) 1024px, (max-width: 1500px) 1500px, 1024px'; | |
return $sizes; | |
} | |
add_filter( 'wp_calculate_image_sizes', 'adjust_image_sizes_attr', 10 , 2 );*/ | |
//* Add custom images instead of default resized from srcset | |
//add_filter( 'wp_calculate_image_srcset', 'ig_add_custom_image_srcset', 10, 5 ); | |
function ig_add_custom_image_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ){ | |
$image_pieces = explode ( '-l.', $image_src); | |
if ( !empty($image_pieces) ) { | |
$image_src_1500 = $image_src; | |
$image_src_900 = $image_pieces[0] . '-m.' . $image_pieces[1]; | |
$image_src_480 = $image_pieces[0] . '-s.' . $image_pieces[1]; | |
$width = array('480', '900', '1500'); | |
//reinstantiate $sources array to empty WP generated srcset | |
$sources = array(); | |
foreach ($width as $w) { | |
$sources[$w] = array( | |
'url' => ${'image_src_'.$w}, | |
'descriptor' => 'w', | |
'value' => $w, | |
); | |
} | |
//var_dump($sources); | |
//return sources with new srcset value | |
return $sources; | |
} //end if | |
} | |
function ig_resp_images() { | |
global $post; | |
$css = ''; | |
if ( have_posts() ) : while ( have_posts() ) : the_post(); | |
$attachments = get_children( array( | |
'post_type' => 'attachment', | |
'post_mime_type' => 'image', | |
'numberposts' => -1, | |
'post_parent' => 278 | |
) ); | |
print_r($attachments); | |
if ( $attachments ) { | |
foreach ( $attachments as $attachment ) { | |
$image_pieces = explode ( '-l.', wp_get_attachment_url( $attachment->ID ) ); | |
if ( !empty($image_pieces) ) { | |
$css .= ' | |
.'.$attachment->ID.' { background-image: url();} | |
'; | |
} | |
} | |
} | |
endwhile; endif; | |
if( !empty( $css ) ) | |
wp_add_inline_style( 'child-theme', $css ); | |
} | |
//add_action( 'wp_enqueue_scripts', 'ig_resp_images' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment