泪雪博客接入百度熊掌号(原百度官方号)已经有一段时间了,虽然目前暂时还无法统计实际的效果,不过但从百度站长平台熊掌号的数据分析中来看还是不错的,前面也已经分享过“WordPress 百度熊掌号快速开发改造教程”,今天就把其中关于熊掌号接入 Json_LD 数据的代码分享出来给大家。

当然如果你对代码一窍不通或者没有心思去折腾,那么子凡还提供了 WordPress 熊掌号接入改造插件:Fanly XZH ,可以快速无需修改代码即可实现 MIP 页面、自适应 H5 页面对熊掌号的支持和改造。

WordPress 百度熊掌号 Json_LD 数据

首先来分享一个最简单的一段 Json_LD 数据代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//获取文章/页面摘要
function fanly_excerpt($len=220){
	if ( is_single() || is_page() ){
		global $post;
		if ($post->post_excerpt) {
			$excerpt  = $post->post_excerpt;
		} else {
			if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){
				$post_content = $result['1'];
			} else {
				$post_content_r = explode("\n",trim(strip_tags($post->post_content)));
				$post_content = $post_content_r['0'];
			}
			$excerpt = preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,0}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$post_content);
		}
		return str_replace(array("\r\n", "\r", "\n"), "", $excerpt);
	}
}
1
2
3
4
5
6
7
8
9
<script type="application/ld+json">{
	"@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld",
	"@id": "<?php the_permalink(); ?>",
 	"appid": "这里请填写熊掌号 ID",
	"title": "<?php the_title(); ?>",
	"images": ["<?php echo catch_that_image() ?>"],
	"description": "<?php echo fanly_excerpt()?>",
	"pubDate": "<?php echo get_the_time('Y-m-d\TH:i:s')?>",
}</script>

其中子凡自定义了一个文章或页面的摘要函数,如果没有设置文章摘要将会自动截取文章第一段的指定长度作为摘要。

下面我们就继续来加强这段代码,百度熊掌号在 Json_LD 数据里面是支持单张缩略图和三张缩略图的样式,当然对于子凡这种力求极致的人来说,不完善这个可能晚上都不能好好睡觉的人必须做到,子凡就直接在完整的一个推荐代码中给大家贴出来吧。

WordPress 百度熊掌号 Json_LD 数据完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//获取文章/页面摘要
function fanly_excerpt($len=220){
	if ( is_single() || is_page() ){
		global $post;
		if ($post->post_excerpt) {
			$excerpt  = $post->post_excerpt;
		} else {
			if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){
				$post_content = $result['1'];
			} else {
				$post_content_r = explode("\n",trim(strip_tags($post->post_content)));
				$post_content = $post_content_r['0'];
			}
			$excerpt = preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,0}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$post_content);
		}
		return str_replace(array("\r\n", "\r", "\n"), "", $excerpt);
	}
}
 
//获取文章中的图 last update 2018/01/22
function fanly_post_imgs(){
	global $post;
	$src = '';
	$content = $post->post_content;  
	preg_match_all('/<img .*?src=[\"|\'](.+?)[\"|\'].*?>/', $content, $strResult, PREG_PATTERN_ORDER);  
	$n = count($strResult[1]);  
	if($n >= 3){
		$src = $strResult[1][0].'","'.$strResult[1][1].'","'.$strResult[1][2];
	}elseif($n >= 1){
		$src = $strResult[1][0];
	}
	return $src;
}

子凡建议大家把以上两段代码添加到你需要接入熊掌号的主题的 functions.php 中,然后继续:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
if(is_single()){
	echo '<script type="application/ld+json">{
	"@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld",
	"@id": "'.get_the_permalink().'",
 	"appid": "这里请填写熊掌号 ID",
	"title": "'.get_the_title().'",
	"images": ["'.fanly_post_imgs().'"],
	"description": "'.fanly_excerpt().'",
	"pubDate": "'.get_the_time('Y-m-d\TH:i:s').'"
}</script>
';}
?>

最后大家就可以把以上这段代码添加在你的 WordPress 主题的 header.php 的合适的位置,以上代码子凡还做了一个 if 判断,只让该段代码在文章中输出。

写在最后

关于 WordPress 接入百度熊掌号的 Json_LD 数据代码改造就给大家分享到这里了吧,最后如果大家的 WordPress 站点成功接入熊掌号,不妨在试试子凡提供的《WordPress 百度熊掌号数据提交插件》,这个插件或许能够助力你更快的实现官方好数据提交和原创推送。

更多关于WordPress优化及疑问可以添加QQ群:255308000

除非注明,否则均为泪雪博客原创文章,禁止任何形式转载

本文链接:https://zhangzifan.com/wordpress-json-ld.html

留言评论

登录 后留言