欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」WordPress 百度搜索落地页时间因子规范开发改造代码

作者 : 自学哈 本文共2501个字,预计阅读时间需要7分钟 2022-11-26 共94人阅读
也想出现在这里? 联系我们

关于百度搜索落地页时间因子规范子凡已经在泪雪博客的分享过开发示例,对于动手能力强的站长或者喜欢折腾的站长可能已经安奈不住了,当然子凡也忍不住第一时间来开始匹配到我们的 WordPress 开源网站程序,由于之前子凡也分享过熊掌号的改造代码,所以时间因子的改造就更简单了。

直接在原来的 WordPress 熊掌号改造代码的 JSON-LD 结构化数据中添加几个百度搜索落地页时间因子规范必要的字段即可,当然以下子凡提供的代码只要官方有说明的字段都统一添加上了,基本可以满足需求,其中子凡任务最重要的一个字段无非就是 upDate 字段,用于给搜索引擎判断内容的最后更新时间。

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
//WordPress 百度落地页时间因子改造代码(header.php 文件中的 head 标签之前)
if(is_single()||is_page()){
	$Url = get_the_permalink();
	$tags = "";foreach(wp_get_post_tags($post->ID) as $tag){$tags = $tags.\'","\'.$tag->name;}$tags=get_the_category()[0]->cat_name.$tags;
	echo \'<script type="application/ld+json">{
	"@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld",
	"@id": "\'.$Url.\'",
	"appid": "这里请填写熊掌号 ID/没有则填\'\'",
	"title": "\'.get_the_title().\'",
	"images": ["\'.fanly_post_imgs().\'"],
	"description": "\'.fanly_custom_excerpt().\'",
	"pubDate": "\'.get_the_time(\'Y-m-d\\TH:i:s\').\'",
	"upDate": "\'.get_the_modified_time(\'Y-m-d\\TH:i:s\').\'",
	"data":{
		"webPage":{
			"headline":"\'.get_the_title().\'",
			"tag":["\'.$tags.\'"],
			"pcUrl":"\'.$Url.\'",
			"wapUrl":"\'.$Url.\'",
			"appUrl":"\'.$Url.\'",
			"mipUrl":"\'.$Url.\'",
			"fromSrc":"\'.get_bloginfo(\'name\').\'",
			"datePublished": "\'.get_the_time(\'Y-m-d\\TH:i:s\').\'"
		}
	}
}</script>
\';}

//以下代码可以添加到当前主题 functions.php 文件中
//获取文章中的图 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;
}
//获取文章/页面摘要
function fanly_custom_excerpt($len=120){
	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);
	}
}
?>

以上就是子凡整理的关于 WordPress 百度搜索落地页时间因子规范的 JSON-LD 结构化数据开发改造的完整代码,不过该代码仅支持 WordPress 的文章和页面,并不支持首页、分类、作者页等,这些功能也都将会在 Fanly XZH 熊掌号改造插件中升级并支持。

如果你有使用子凡提供的熊掌号改造代码,可以通过更新以上代码支持时间因子规范,如果你已经使用 Fanly XZH 熊掌号改造插件,那么请继续的稍作等待和保持关注泪雪博客和插件更新。

本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」WordPress 百度搜索落地页时间因子规范开发改造代码
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号