admin管理员组文章数量:1125113
I'm using W3 Total Cache plugin for my WordPress site. I've enabled Lazy Load setting option, but I want to disable the option for the specific post type. How can I hook and disable it?
I'm using W3 Total Cache plugin for my WordPress site. I've enabled Lazy Load setting option, but I want to disable the option for the specific post type. How can I hook and disable it?
Share Improve this question asked Jan 1, 2020 at 9:03 Kimsea SokKimsea Sok 171 silver badge6 bronze badges3 Answers
Reset to default 2According to the W3 Total Cache plugin author's website, you can prevent individual images from lazy loading if you add the image URLs to the "Exclude words" section of the User Experience > Lazy Loading section.
This is helpful when using page builders that do not give you direct access to the class attribute of the image tag (such as Elementor).
This answer might be a bit too late but here goes: W3 Total Cache will skip lazy loading images that have class "no-lazy". This means that you can hook to the get_the_post_thumbnail() filter post_thumbnail_html and add the class to the image.
Here's an example:
/**
* Disable W3 Total Cache lazy load for post type "post"
*
* @param string $html
* @param int $post_id
* @param int $image_id
* @param string|int[] $size
* @param string|array $attr
*/
function _post_thumbnail_html( $html, $post_id, $image_id, $size, $attr ){
if( !empty( $html ) ){
$post = get_post( $post_id );
if( 'post' === $post->post_type ){
if( isset( $attr['class'] ) ){
$attr['class'] .= ' no-lazy';
}else{
if( !is_array( $attr ) ){
$attr = array();
}
$attr['class'] = 'no-lazy';
}
$html = wp_get_attachment_image( $image_id, $size, false, $attr );
}
}
return $html;
}
add_filter( 'post_thumbnail_html', '_post_thumbnail_html', 10, 5 );
You can use this filter,
function ar_lazyload_deactivate() { if ( is_singular( 'posttype name' ) ) { add_filter( 'do_rocket_lazyload', '__return_false' ); } } add_filter( 'wp', __NAMESPACE__ . '\ar_lazyload_deactivate' );
本文标签: pluginsHow can I disable W3 Total Cache Image Lazy Load for Specific Post Type
版权声明:本文标题:plugins - How can I disable W3 Total Cache Image Lazy Load for Specific Post Type? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736658335a1946314.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论