bis_get_attachment_picture doesn’t have cropping for default image
-
bis_get_attachment_picture doesn’t support cropping for default image, which is required in my case. I wrote custom function by modifying bis_get_attachment_picture. Instead of number you can pass “default” as break-point and it is used as default settings . Maybe you can add it to the plugin code, here is my code:
function custom_bis_get_attachment_picture( $attachment_id = 0, $sizes = array(), $attr = array() ){ if( $attachment_id < 1 || ! is_array( $sizes ) || count( $sizes ) === 0 ){ return ''; } ksort( $sizes ); $last_breakpoint = 0; $html = '<picture>'; $default_srcset = ""; foreach( $sizes as $breakpoint => $data ){ if( (intval( $breakpoint ) || $breakpoint == 'default') && count( $data ) >= 2 ){ $img = bis_get_attachment_image_src( $attachment_id, array( $data[0], $data[1] ), isset( $data[2] ) ? $data[2] : false ); if( $img ){ if( isset( $attr['retina'] ) && $attr['retina'] ){ $retina = bis_get_attachment_image_src( $attachment_id, array( $data[0] * 2, $data[1] * 2 ), isset( $data[2] ) ? $data[2] : false ); $srcset = $img['src'] . ' 1x, ' . $retina['src'] . ' 2x'; }else{ $srcset = $img['src']; } if ($breakpoint == 'default') { $default_srcset = $srcset; } else { $html .= '<source media="(max-width:' . intval( $breakpoint ) . 'px)" srcset="' . $srcset . '">'; $last_breakpoint = intval( $breakpoint ); } } } } $html .= '<source media="(min-width:' . ( $last_breakpoint + 1 ) . 'px)" srcset="' . (empty($default_srcset) ? wp_get_attachment_url( $attachment_id ) : $default_srcset) . '">'; if (empty($default_srcset)) { $html .= wp_get_attachment_image( $attachment_id, 'full', false, $attr ); } else { $html .= bis_get_attachment_image($attachment_id, [$sizes['default'][0], $sizes['default'][1]], $sizes['default'][2], $attr); } $html .= '</picture>'; return apply_filters( 'bis_get_attachment_picture', $html, $attachment_id, $sizes, $attr ); }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘bis_get_attachment_picture doesn’t have cropping for default image’ is closed to new replies.