【发布时间】:2014-01-10 22:22:13
【问题描述】:
我正在尝试为客户端实现延迟加载 wordpress 插件到站点,插件是“BJ Lazy Load”,客户端使用的主题是“Viewfinder”。
主页有 2400 个 120x120 的缩略图,它们都是帖子,缩略图是帖子的附件,全尺寸图像(点击时加载)是帖子的特色图像(例如,请参见取景器页面上的图像.)
我安装了插件,但默认情况下,这只会延迟加载帖子内容中的图像,而这些不是。在“其他说明”中,开发人员说通过过滤器传递以下内容以对所有图像使用延迟加载:
<?php
$img_html = '<img src="myimage.jpg" alt="">';
$img_html = apply_filters( 'bj_lazy_load_html', $img_html );
echo $img_html;
?>
我在支持论坛上询问,因为另一个用户遇到了问题,我最终使用了他的代码,并将其粘贴到 functions.php 文件中:
$args = array(
'post_type' => 'attachment',
'post_status' => null,
'post_parent' => $fr_ID,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$attachments = get_posts( $args );
if( $attachments ) {
$thumb_images = array();
foreach( $attachments as $key => $attachment ) {
$thumb_image = wp_get_attachment_image_src( $attachment -> ID, 'thumbnail' );
$thumb_images[] = $thumb_image[0];
}
natsort( $thumb_images );
}
// Apply lazy load filter to images
$img_lazy_load = '<img src="'.$thumb_images[0].'" alt="'.esc_attr( get_the_title() ).'" />';
$img_lazy_load = apply_filters( 'bj_lazy_load_html', $img_lazy_load );
echo $img_lazy_load;
无论我尝试什么,我似乎都无法让这个懒惰的负载工作。有没有人有任何建议、其他方法或我可以尝试的替代插件?
谢谢
【问题讨论】:
标签: php jquery wordpress lazy-loading