WordPress 熊掌号精选问答页面改造代码

张子凡 WordPress优化/熊掌SEO运营 2018-07-22 20:28:16 阅读(...) 评论(5)

前面子凡已经分享过非常完整的熊掌号精选问答页面改造的示例代码,那么作为第一批尝鲜吃螃蟹的人,子凡的泪雪网目前已经积极在做数据推送,并且数据目前相关数据也在开始生效,同时也作为 WordPress 的深度开发爱好者,子凡决定分享一下 WordPress 熊掌号精选问答页面的改造代码。

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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
global $wp;$current_url = home_url(add_query_arg(array(),$wp->request));
if($current_url)echo '<link rel="canonical" href="'.$current_url.'" />'."\n";
$xzh_title=$xzh_upDate=$xzh_pubDate=$xzh_lrDate=$images=$description=$xzh_data='';
if(is_single()||is_page()){
	$xzh_title = get_the_title();
	$current_url = get_the_permalink();
	$images = '"images": ["'.fanly_post_imgs().'"],'."\n";
	$description = '"description": "'.fanly_custom_excerpt().'",'."\n";
	$xzh_pubDate = '"pubDate": "'.get_the_time('Y-m-d\TH:i:s').'",'."\n";
	if(get_comments(array('post_id'=>$GLOBALS['post']->ID,'status'=>'approve','count'=>true))){
		$xzh_upDate = '"upDate": "'.get_the_modified_time('Y-m-d\TH:i:s').'",'."\n";
		if(in_category(array(1377))){//熊掌号问答内容,1377 为相关问答分类目录 ID,多个可用英文,逗号分隔。
			$xzh_lrDate = '"lrDate": "'.date('Y-m-d\TH:i:s',strtotime(get_comments(array('post_id'=> $GLOBALS['post']->ID,'status' => 'approve','number'=>1))[0]->comment_date)).'",'."\n";
		}else{
			$xzh_lrDate = '"lrDate": "'.date('Y-m-d\TH:i:s',strtotime(get_comments(array('post_id'=> $GLOBALS['post']->ID,'status' => 'approve','number'=>1))[0]->comment_date)).'"'."\n";
		}
	}else{
		if(in_category(array(1377))){//熊掌号问答内容
			$xzh_upDate = '"upDate": "'.get_the_modified_time('Y-m-d\TH:i:s').'",'."\n";
		}else{
			$xzh_upDate = '"upDate": "'.get_the_modified_time('Y-m-d\TH:i:s').'"'."\n";
		}
	}
 
//熊掌号问答内容 S
if(in_category(array(1377))){//1377 为相关问答分类目录 ID,多个可用英文,逗号分隔。
	$headline = strtr($xzh_title,array(','=>'','?'=>'','!'=>'',','=>'','!'=>'','?'=>'','、'=>'','“'=>'','”'=>''));
	switch( get_the_author_meta('user_level',$GLOBALS['post']->post_author) ){
		case 10: $jobTitle = '主编';  break;  //管理
		case 7: $jobTitle = '编辑';  break;  
		case 2: $jobTitle = '作者';  break;  
		case 1: $jobTitle = '投稿';  break;  
		case 0: $jobTitle = '用户';  break;  
	}
   $xzh_data = '"data": {
	"WebPage": {
		"headline": "'.$headline.'",
		"fromSrc": "泪雪网",
		"domain": "科技",
		"category": [
			"问答"
		],
	},
	"Question": [
		{
			"acceptedAnswer": "'.fanly_custom_excerpt().'"
		}
	],
	"ImageObject": [
		{
			"contentUrl": "'.fanly_post_imgs().'",
			"scale": "5:2"
		}
	],
	"Author": [
		{
			"name": "'.get_the_author_meta('display_name',$GLOBALS['post']->post_author).'",
			"memberOf":
				{
					"legalName": "泪雪网"
				},
			"jobTitle": ["'.$jobTitle.'"],
			"headPortrait": "'.get_avatar_url( get_the_author_meta('email') ).'"
		}
	]
}';
}
//熊掌号问答内容 E
}
if($current_url && $xzh_title && $xzh_pubDate){
echo '<script type="application/ld+json">{
"@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld",
"@id": "'.$current_url.'",
"appid": "你的熊掌号 APPID 填在这里",
"title": "'.$xzh_title.'",';
echo $images.$description.$xzh_pubDate.$xzh_upDate.$xzh_lrDate.$xzh_data;
echo '}</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 改造熊掌号精选问答的代码,但是以上代码仅仅只是针对图文的问答,也就是说不支持视频或者音频类的问答,同时以上的部分代码是中的部分字段都是固定的,例如熊掌号 ID,站点名称,领域等,根据实际情况填写修改。

当然以上代码也只能供大家参考和快速接入改造熊掌号专业问答,但是最后还需要的就是主动提交,当然子凡也开发了相关的熊掌号推送数据插件,但是目前免费版的 Fanly Submit 熊掌号推送插件还并没有支持熊掌号精选问答的专业问答推送,不过子凡目前自己用于泪雪网的插件也升级开发过,目前还在测试阶段,如果有人需要可以联系我。

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

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

本文链接:https://zhangzifan.com/wordpress-xzh-ask.html

留言评论

登录 后留言