博主接入熊掌号也有一段时间了 之前就改造过 不过前一段时间我发现 我只改造了资源提交 没有改造 搜索出图
近期又改造了下 感觉很好
该次改造不仅适配了熊掌号搜索出图 还搞定了季风算法和极光算法
而JSON-LD也有利于谷歌等搜索引擎识别网页内容。因此改造的利好多多,所以呢这篇文章将一同介绍JSON-LD与百度熊掌号改造
JSON-LD的改造
JSON-LD或是熊掌号其实都是可以通过插件改造的 而且插件还和主题兼容性有关 我感觉 插件太多没啥好处 还是精简些比较好
你需要在主题的编辑中的模板函数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('/<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); } }
//获取文章中的图 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; }
然后就是在头部改造JSON-LD啦 编辑header.php 在<head>标签前面插入
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').'" "upDate": "'.get_the_modified_time('Y-m-d\TH:i:s').'",//极光算法 "data": { "WebPage": { "title": "'.get_the_title().'",//极光算法 "pcUrl": "'.get_the_permalink().'",//极光算法 "wapUrl": "'.get_the_permalink().'"//极光算法 } } }</script> ';}
该代码还做了一个IF判断 让其只在文章页输出 以免发生不必要的抓取 影响整体收录
代码内的upDate、WebPage、pcURL、wapURL等参数是为极光算法添加的,如果只需要进行熊掌号改造,可以删除这四个参数。
Data这部分参数还有很多扩展空间,谷歌等搜索引擎都有关于它的架构建议,有兴趣可以搜索JSON-LD查看。
以上就是JSON-LD改造方法了