谭波最近在网站中管理的一个WordPress站点进行了百度熊掌号改造,实现起来也并不是那么麻烦。下面成都seo博客谭波就把代码分享给需要对wordpress进行百度熊掌号页面改造的朋友。
第一:百度熊掌号接入要求
在百度资源网站(http://ziyuan.baidu.com/)进行开通熊掌号权限。具体操作方法请点击百度资源网站导航栏的“熊掌号”,有详细的操作流程和方法。如果没有权限下面的操作都是没有用的。所以百度熊掌号页面改造需要开通权限!
第二:在wordpress站点放置代码
1、在WordPress站点的header.php文件前面放置下面的代码
添加canonical标签
2、在header.php或者footer.php添加JSON_LD数据
if(is_single()){
echo '
"@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld",
"@id": "'.get_the_permalink().'",
"appid": "你的appid",
"title": "'.get_the_title().'",
"images": ["'.fanly_post_imgs().'"],
"description": "'.fanly_excerpt().'",
"pubDate": "'.get_the_time('Y-m-d\TH:i:s').'"
}
3、在functions.php底部添加下面的代码
function fanly_excerpt($len=220){
if ( is_single() || is_page() ){
global $post;
if ($post->post_excerpt) {
$excerpt = $post->post_excerpt;
} else {
if(preg_match('/
(.*)/iU',trim(strip_tags($post->post_content,"
")),$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);
}
}
//优先获取文章中的三张图
function fanly_post_imgs(){
global $post;
$content = $post->post_content;
preg_match_all('//', $content, $strResult, PREG_PATTERN_ORDER);
$n = count($strResult[1]);
if($n >= 3){
$src = $strResult[1][0].'","'.$strResult[1][1].'","'.$strResult[1][2];
}else{
if( $values = get_post_custom_values("thumb") ) { //输出自定义域图片地址
$values = get_post_custom_values("thumb");
$src = $values [0];
} elseif( has_post_thumbnail() ){ //如果有特色缩略图,则输出缩略图地址
$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
$src = $thumbnail_src [0];
} else { //文章中获取
if($n > 0){ // 提取首图
$src = $strResult[1][0];
}
}
}
return $src;
}
?>
第三步:在WordPress站点添加关注功能代码
//吸顶bar
//文章段落间bar
//底部bar
当然这个代码是可以不用添加的,看自己的网站情况而定吧!不过成都seo谭波建议最好还是添加为妙。