【www.xieat.com - 新闻】
由于上传的图片一般会很大,加载起来速度比较点,一般都会在首页和分类页获取图片的缩略图。
wordpress原来的用于获取系统自动生成的缩略图的方法
function catch_that_image() {
//新加入取得小尺寸缩略图
global $post;
$args = array(
"numberposts" => 1,
"order"=> "ASC",
"post_mime_type" => "image",
"post_parent" => $post->ID,
"post_status" => null,
"post_type" => "attachment"
);
$attachments = get_children($args);
$first_img = "";
if($attachments) {
$image = array_pop($attachments);
$imageSrc = wp_get_attachment_image_src($image->ID, "thumbnail");
$first_img = $imageSrc[0];
} else {
$random = mt_rand(1, 20);
$first_img = get_bloginfo("template_url"). "/images/random/tb".$random.".jpg";
}
//新加入取得小尺寸缩略图
return $first_img;
}
在使用又拍云后,把以上的代码改了一下,用于取得又拍云存储上的缩略图改为如下
//获取缩略图
function catch_that_image() {
//新加入取得小尺寸缩略图
global $post, $posts;
$first_img = "";
ob_start();
ob_end_clean();
$output = preg_match_all("/
$first_img = $matches [1] [0];
if(empty($first_img))
{
$random = mt_rand(1, 20);
$first_img = get_bloginfo("template_url"). "/images/random/tb".$random.".jpg";
return $first_img;
}
else
{
$first_img=$first_img."!150x100";
return $first_img;
}
}
这里记录一下,为了以后万一不在使用又拍云时可以改回来,需要注意的是这一句
$first_img=$first_img."!150x100";
"!150x100"是你又拍云的缩略图后缀名哦。一般由 当前设置的间隔标志符+版本名称 组成
还有这一句
$first_img = get_bloginfo("template_url"). "/images/random/tb".$random.".jpg";
是用于在文章没有图片时取得随机图片
本文来源:http://www.xieat.com/xinwen/27785/
推荐访问:wordpress缩略图插件