今天彻底清除掉了省略图插件,虽然纯代码代替了这个插件,但我发现还是有好多图不会显示。这说明这个省略图应用不仅不会给网站带来访问速度,还会影响访问友好度。于是我做了以下更改:
0、添加图片预加载JS代码,代码如下:(放在JS文件内)添加后网站访问速度快了许多。
var imgReady = (function(){
var list = [],
intervalId = null;// 用来执行队列
var queue = function(){for(var i = 0; i < list.length; i++){
list[i].end ? list.splice(i--,1) : list[i]();
}
!list.length && stop();
};// 停止所有定时器队列
var stop = function(){
clearInterval(intervalId);
intervalId = null;
}
return function(url, ready, error) {
var onready = {},
width,
height,
newWidth,
newHeight,
img = new Image();
img.src = url;// 如果图片被缓存,则直接返回缓存数据
if(img.complete) {
ready.call(img);
return;
}
width = img.width;
height = img.height;// 加载错误后的事件
img.onerror = function () {
error && error.call(img);
onready.end = true;
img = img.onload = img.onerror = null;
};// 图片尺寸就绪
var onready = function() {
newWidth = img.width;
newHeight = img.height;
if (newWidth !== width || newHeight !== height ||
// 如果图片已经在其他地方加载可使用面积检测
newWidth * newHeight > 1024
) {
ready.call(img);
onready.end = true;
};
};
onready();
// 完全加载完毕的事件
img.onload = function () {
// onload在定时器时间差范围内可能比onready快
// 这里进行检查并保证onready优先执行
!onready.end && onready();
// IE gif动画会循环执行onload,置空onload即可
img = img.onload = img.onerror = null;
};// 加入队列中定期执行
if (!onready.end) {
list.push(onready);
// 无论何时只允许出现一个定时器,减少浏览器性能损耗
if (intervalId === null) {
intervalId = setInterval(queue, 40);
};
};
}
})();
1、设置省略图裁剪 小图为150x150 中图为 300*300 最大设置为 0.目的是为了方便调用。列表省略图采用小图,幻灯采用中图。
2、就是添加两个件,thumbnail.php(列表页和首页调用)和thumbnail_index.php(相关文章调用),准确的来说一个添加了.thumbnail标签另一个没有。因为样式表中thumbnail是设置了方向了的。
代码如下:inc/thumbnail_index.php
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
<?php if (has_post_thumbnail()) { the_post_thumbnail('thumbnail', array('alt' => get_the_title()));
} else { ?>
<img class="home-thumb" src="<?php echo catch_image() ?>" alt="<?php the_title(); ?>" />
<?php } ?></a>
文件路径:inc/thumbnail.php 首页列表专用
<?php if (has_post_thumbnail()) {?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
<?php the_post_thumbnail('thumbnail',array('class' => thumbnail));?></a>
<?php } else { ?>
<?php
$szPostContent = $post->post_content;
$szSearchPattern = '~<img [^>]* />~'; // 搜索所有符合的图片
preg_match_all($szSearchPattern, $szPostContent, $aPics);
$iNumberOfPics = count($aPics[0]); // 检查一下至少有一张图片
?>
<?php if ($iNumberOfPics > 0) {?>
<a class="thumbnail" href="<?php the_permalink();?>" title="<?php the_title(); ?>"><?php echo $aPics[0][0] ;?></a>
<?php } ?>
<?php } ?>
参考资料:
the_post_thumbnail可以是字符串或数组
a.字符串参数时:thumbnail(小尺寸)、medium(中等尺寸)、large(大尺寸)、full(完整尺寸)
<?php the_post_thumbnail( 'thumbnail' ); ?> 表示小尺寸,依次类推
b.数组参数
//尺寸60x60
<?php the_post_thumbnail( array(60,60) ); ?>
c.默认尺寸:
<?php the_post_thumbnail(); ?>
栖息邦 收藏Ctrl+D 转载注明来源