QQ发出去一个网址后,在展示的时候,他会快速抓取网站的内容(标题,缩略图,描述)进行展示,抓取有特殊标记的内容,快速展示出来。我们可以通过在head部分加标签的方式让这个过程更快,更准确的展示我们需要的内容。
<!--必填-->
<meta itemprop="name" content="网站标题"/>
<meta itemprop="image" content="网站要显示的图片链接" />
<meta name="description" itemprop="description" content="网站内容" />
将以上标签加至文章head即可。
name = 文章标题
image = 特色图(未设置特色图自动抓取文章第一张图片)
meta = 文章描述
name = 分类名称
image = 自定义
meta = 分类描述
name = 站点名称
image = 站点logo
meta = 站点描述
由于主题不一样,我这里只给出我主题的部分代码,至于缩略图获取,文章描述获取等这类函数就不一一贴出。
<meta name="description" itemprop="description" content="<?php description(); ?>"><meta itemprop="image" content="<?php echo ravenclaw_share_post_image(); ?>" />
加入header.php
function ravenclaw_share_post_image(){
global $post;
if (has_post_thumbnail($post->ID)) /*有特色图就直接返回特色图*/
{
$post_thumbnail_id = get_post_thumbnail_id( $post );
$img = wp_get_attachment_image_src( $post_thumbnail_id, 'full' );
$img = $img[0];
}
else/*否则抓取第一张图片*/
{
$content = $post->post_content;
preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
if (!empty($strResult[1])) {
$img = $strResult[1][0];
}else{
$img = ravenclaw_option('tool_single_image');
}
};
if (is_home())/*首页图片返回站点logo*/
{
$img = ravenclaw_option('tool_share_image');
}
return $img;
}
思路说了,完整代码可以根据自己主题函数进行完善
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。