WordPress熊掌号页面改造,自动发布

写在前面的话:

有很多小伙伴刚进入WordPress,对很多东西还不太了解,比如:有的主题很挑剔,对于有些插件不兼容,但是呢对于这个功能有不可或缺。所以,这时候就需要我们自己手动修改或者添加代码,来实现我们想要的功能。

 

比如,百度推出的熊掌号,对于站长们来说这是不得不去使用的一个功能,一方面是推广的需求,另一方面也是为了保护原创作品。

要使用熊掌号的话,我们的站点必须满足熊掌号的“协议”,通俗的将也就是页面改造

 

今天,就给大家介绍一下如何免插件实现熊掌号页面改造

页面改造分为两部分:

  1. h5页面改造
  2. 粉丝关注改造

h5页面改造需要在文章的header部位操作,也就是网站的头部信息,对于WordPress来讲,网站header包含了head与body标签,所以,对于WordPress改造来说,我们只需要修改网站的header.php文件即可。看下图:

熊掌号收录的是有价值的原创作品,一般都是以文章格式存在的,所以,我们在做WordPress改造的时候需要做一个判断:当前页面是不是文章页面,如果是的话就按照熊掌号要求的格式提交数据,如果不是就不提交。

请看如下代码:

//获取文章/页面摘要
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;
}

//熊掌号h5页面改造
function fanly_h5(){
if(is_single()){
echo <link rel="canonical" href="<?php the_permalink(); ?>" />
echo '<script src="//msite.baidu.com/sdk/c.js?appid=自己的熊掌号ID"></script>';
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主题的functions.php文件中(复制粘贴到<?php 标签后面保存即可)

然后在header.php中找到</head>标签,在</head>标签前面加上<?php fanly_h5(); ?>

如图:

以上就是WordPress熊掌号h5页面改造的全过程!

 

文章数据自动提交熊掌号

如果我们一个一个提交文章数据到熊掌号的话,这样不单浪费时间还浪费精力。

这里有一个插件可以帮助大家解决这个问题,当发布文章的时候自动提交数据,是不是觉得很赞呢!

下载地址如下:

 

WordPress插件的安装方法请自行到百度搜索

 

<--End-->

 

固定链接: https://adcc.me/676.html

posted @ 2018-04-19 16:15  永云  阅读(1510)  评论(0编辑  收藏  举报