该楼层疑似违规已被系统折叠隐藏此楼查看此楼
这个方法无需用timthumb.php,利用系统本身的缩略图剪切功能做到,自动选择固定大小缩略图,当没有设置特色图片时,自动捉去文章第一张图片的缩略图,当即没有特色图片也没有文章图片时,随机输出图片;
首先在function.php里面加上如下代码
function catch_first_image(){
global$post,$posts;
$first_img='';
ob_start();
ob_end_clean();
$output = preg_match_all('//i', $post->post_content, $matches);
$first_img = preg_replace("/\.jpg$/", "-171x171.jpg", $matches [1] [0]);//171x171.jpg为媒体库设定缩略图大小;
if(empty($first_img)){
$random= mt_rand(1, 10);
echo get_bloginfo ( 'stylesheet_directory' );
echo '/img/'.$random.'.jpg'; //在img文件夹里放10张jpg结尾的图片,以数字1-10命名;
}
return $first_img;
}
然后在要输出图片的位置写入一下代码:
//图片超级链接,点击打开文章
<?php the_post_thumbnail(array(171,171) ); ?> //输出特色图片
其中the_post_thumbnail参数
the_post_thumbnail('thumbnail'); // Thumbnail 输出最小缩略图
the_post_thumbnail('medium'); // Medium resolution 中等
the_post_thumbnail('large'); // Large resolution 大图
the_post_thumbnail('full'); // Full resolution 完整图片
因为wp中缩略图的文件名格式为: 图片名-剪裁的大小.jpg
所以利用正则表达式获取图片后,在地址后面添加"-171x171.jpg"然后再输出图片
这样就不会因为图片大小影响输出效果;
/?p=1053