700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > wp缩略图php wordpress缩略图彻底解决方法 自动获取第一张图片缩略图

wp缩略图php wordpress缩略图彻底解决方法 自动获取第一张图片缩略图

时间:2020-05-26 20:34:24

相关推荐

wp缩略图php wordpress缩略图彻底解决方法 自动获取第一张图片缩略图

该楼层疑似违规已被系统折叠隐藏此楼查看此楼

这个方法无需用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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。