Quiero desactivar el srcset solo al llamar a una miniatura específica
tamaño (por ejemplo, solo cuando se llama tamaño de imagen completo).
Aquí hay dos ideas (si te entiendo correctamente):
Enfoque # 1
Verifiquemos el tamaño del filtro post_thumbnail_size
. Si coincide con un tamaño correspondiente (por ejemplo, full
), nos aseguramos de que el $image_meta
esté vacío, con el filtro wp_calculate_image_srcset_meta
. De esa manera, podemos salir temprano de la función wp_calculate_image_srcset()
(antes de usar los filtros max_srcset_image_width
o wp_calculate_image_srcset
para deshabilitarla):
/**
* Remove the srcset attribute from post thumbnails
* that are called with the 'full' size string: the_post_thumbnail( 'full' )
*
* @link http://wordpress.stackexchange.com/a/214071/26350
*/
add_filter( 'post_thumbnail_size', function( $size )
{
if( is_string( $size ) && 'full' === $size )
add_filter(
'wp_calculate_image_srcset_meta',
'__return_null_and_remove_current_filter'
);
return $size;
} );
// Would be handy, in this example, to have this as a core function ;-)
function __return_null_and_remove_current_filter ( $var )
{
remove_filter( current_filter(), __FUNCTION__ );
return null;
}
Si tenemos:
the_post_thumbnail( 'full' );
entonces la etiqueta <img>
generada no contendrá el atributo srcset
.
Para el caso:
the_post_thumbnail();
podríamos coincidir con la cadena de tamaño 'post-thumbnail'
.
Enfoque # 2
También podríamos agregar / eliminar el filtro manualmente con:
// Add a filter to remove srcset attribute from generated <img> tag
add_filter( 'wp_calculate_image_srcset_meta', '__return_null' );
// Display post thumbnail
the_post_thumbnail();
// Remove that filter again
remove_filter( 'wp_calculate_image_srcset_meta', '__return_null' );