Skip to content

Instantly share code, notes, and snippets.

@torounit
Created October 7, 2014 02:48
Show Gist options
  • Save torounit/f0928f36a9a7273bb6a1 to your computer and use it in GitHub Desktop.
Save torounit/f0928f36a9a7273bb6a1 to your computer and use it in GitHub Desktop.
投稿サムネイルをretina対応する。
<?php
add_image_size("my-image-size", 300, 200, true);
add_image_size("my-image-size@2x", 600, 400, true);
add_filter("post_thumbnail_html",function( $html, $post_id, $post_thumbnail_id, $size, $attr ){
$retina_size = $size."@2x";
if(!has_image_size($retina_size)) {
return $html;
}
if( $image = image_downsize($post_thumbnail_id, $retina_size) and $image[3] ) {
$srcset_image = wp_get_attachment_image_src( get_post_thumbnail_id(), $retina_size );
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, wp_parse_args($attr, [ "srcset" => $srcset_image[0] . ' 2x']) );
}
return $html;
}, 10, 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment