亲测有效:让Wordpress自动引用文章第一张图片为缩略图

已收录   阅读次数: 1,487
2019-07-1410:33:24 发表评论
摘要

wordpress主题设置文章缩略图必须上传到本地服务器,起初不会有什么影响,但是长远来看,服务器储存的图片越来越多,消耗的资源也会越来越多,所以还是以引用外链图片来做缩略图比较好……

分享至:
亲测有效:让Wordpress自动引用文章第一张图片为缩略图

开篇寄语

wordpress主题设置文章缩略图必须上传到本地服务器,起初不会有什么影响,但是长远来看,服务器储存的图片越来越多,消耗的资源也会越来越多,所以还是以引用外链图片来做缩略图比较好……

实现方法

1.在自己的wordpress模板中找到模板函数文件functions.php;

//调用缩略图
function get_first_image() {
global $post;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = bloginfo('template_url') . "/images/default.jpg";
};
return $first_img;
}

2.在网站模板的任意一个模板中,只要需显示缩略图片位置,使用下面的调用代码来调用缩略图。一般是在blog-list内,搜索thumbnail就可以看到了,建议备份源代码。

<?php if ( has_post_thumbnail() ) { the_post_thumbnail(array(200,200),array('alt'=> trim(strip_tags( $post->post_title ))));} else {?><img src="<?php echo get_first_image(); ?>" alt="<?php the_title(); ?>" width="200px" height="200px"/><?php }?>

上面的尺寸可改,比如300px,那么就将上方代码的200,通通换成300就可以了,但是在实际操作中,我的主题似乎并没有更改,只好自己调的css样式,这个需要留意。

亲测有效:让Wordpress自动引用文章第一张图片为缩略图
  • 我的微信
  • 微信扫一扫加好友
  • weinxin
  • 我的微信公众号
  • 扫描关注公众号
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: