WordPress分享微信朋友圈缩略图设置

微信朋友圈缩略图
微信朋友圈缩略图

我们在制作好Wordpress网站以后,希望通过微信朋友圈分享给大家。但是发现这样分享到微信朋友圈是没有小图标的。如何在链接前增加吸引人的图片呢?

微信分享

1、将以下代码粘贴到functions.php文件中。

function catch_that_image() {
global $post, $posts;
$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 = "/你的图片路径";
 
//请自行设置一张default.jpg图片
}
 
return $first_img;
}

2、在网页的头部加上以下代码,图片路径自行修改。

<head>
<div id="wx_pic" style="margin:0 auto;display:none;width:300px;height:300px;">
<img src="<?php echo catch_that_image() ?> " width="300px" height="300px"> 
</div> 
</head>

3、制作像素是 300*300px的图片,命名为default.jpg。将图片放到媒体库。

好文分享

300px*300px

4、保存好文件后,就可以分享到朋友圈了。此时分享的网页链接就会有小图片显示了。

注意事项:尺寸必须是300*300像素,否则在iphone手机上很可能显示不出来。

滚动至顶部