浅谈WordPress接入熊掌号及页面改造

最近想搞个熊掌号来玩玩,于是在前两天申请并且通过.

申请通过后需要对网站进行改造,于是翻阅互连网,东拼西凑终于搞定,完成改造,下面分享下页面改造过程.

方法/步骤

改造之前需注册熊掌号,点此打开,申请次数有限,申请时注意手持身份证照片最好是原图,避免系统识别不到身份证信息.

不同WordPress主题改造文件有所不同,这里我当前使用知更鸟Lts版本,需用到的3个文件分别为functions.phphead.phpcontent.php

添加熊掌号ID声明

首先使用到的是head.php,添加熊掌号ID声明,代码如下:

  1. <script src="//msite.baidu.com/sdk/c.js?appid=此处填写熊掌号ID"></script>

添加JSON_LD数据

  1. <!--Start-->
  2. <!--熊掌号-->
  3. <script src="//msite.baidu.com/sdk/c.js?appid=1595281659287879"></script>
  4. <script type="application/ld+json">
  5.         {
  6.             "@context""https://ziyuan.baidu.com/contexts/cambrian.jsonld",
  7.             "@id""<?php the_permalink(); ?>",
  8.             "appid""熊掌号ID",
  9.             "title""<?php the_title(); ?>",
  10.             "images": ["<?php echo fanly_post_imgs();?>"],
  11.             "description""<?php echo fanly_excerpt()?>",
  12.             "pubDate""<?php echo get_the_time('Y-m-d\TH:i:s')?>"
  13.         }
  14. </script>
  15. <!--end-->

functions.php

  1. //Start
  2. //获取文章/页面摘要
  3. function fanly_excerpt($len=220){
  4.     if ( is_single() || is_page() ){
  5.         global $post;
  6.         if ($post->post_excerpt) {
  7.             $excerpt  = $post->post_excerpt;
  8.         } else {
  9.             if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){
  10.                 $post_content = $result['1'];
  11.             } else {
  12.                 $post_content_r = explode("\n",trim(strip_tags($post->post_content)));
  13.                 $post_content = $post_content_r['0'];
  14.             }
  15.             $excerpt = preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,0}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$post_content);
  16.         }
  17.         return str_replace(array("\r\n""\r""\n"), ""$excerpt);
  18.     }
  19. }
  20. //获取缩略图
  21. function fanly_post_imgs(){
  22.     global $post;
  23.     $src = '';
  24.     $content = $post->post_content;
  25.     preg_match_all('/<img .*?src=[\"|\'](.+?)[\"|\'].*?>/', $content, $strResult, PREG_PATTERN_ORDER);  
  26.     $n = count($strResult[1]);  
  27.     if($n >= 3){
  28.         $src = $strResult[1][0].'","'.$strResult[1][1].'","'.$strResult[1][2];
  29.     }elseif($n >= 1){
  30.         $src = $strResult[1][0];
  31.     }
  32.     return $src;
  33. }
  34. //END

content.php

以下代码不同主题可能有所不同,根据实际情况添加适当位置,本站当前在105行处添加,文件目录为主题目录/template/content.php

  1. <script>cambrian.render('tail')</script>

效果展示

效果显示在文章末端
浅谈WordPress接入熊掌号及页面改造

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
搜索