禁用特定页面模板的srcset

时间:2017-12-22 14:07:55

标签: wordpress responsive srcset

我正在尝试在Wordpress中禁用特定页面模板中的reponsive图像(这些模板用于srcset不合适的自定义RSS Feed)。

add_filter( 'wp_calculate_image_srcset', '__return_false' );
主题的functions.php中的

可以处理所有图像的工作。

但是一旦我使用条件标签,它就会完全停止工作,例如:

if ( is_page_template( 'page-mytemplate.php' ) ) {
add_filter( 'wp_calculate_image_srcset', '__return_false' );
}

任何想法如何让它发挥作用?

1 个答案:

答案 0 :(得分:0)

我正在尝试做同样的事情。仅在商店目录产品缩略图上关闭 srcset,但在所有页面上关闭。

(我在拇指悬停时增加了缩放图像效果并且它悬停在低质量的srcset)

function m_disable_srcset( $size ) {
if ( $size = 'shop_catalog' ) {
return false;
}}
add_filter( 'wp_calculate_image_srcset', 'm_disable_srcset' );

编辑:此代码有效:

/**
 * 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 ) && 'shop_catalog' === $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;
}