wordpress知更鸟begin主题百度熊掌号结构化改造

  • A+
所属分类:编程语言

wordpress知更鸟主题begin百度熊掌号结构化改造接入方法较为简单,主要分为如下三步。当然在改造之前需要到百度去注册一个熊掌号的账号并从熊掌号设置中获取ID,形如:

熊掌号ID:1580333068982852

wordpress知更鸟begin主题百度熊掌号结构化改造

一、header.php头部改造

将如下代码如下代码放入/wp-content/themes/begin/header.php文件中</head>标签前面,同时注意有两处要将熊掌号ID换成自己的。

  1. <!--Start-->
  2. <!--熊掌号-->
  3. <script?src="//msite.baidu.com/sdk/c.js?appid=自己的熊掌号ID"></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改造

将如下代码放入/wp-content/themes/begin/functions.php文件最后面

  1. //2018年5月16日添加百度熊掌号改造,获取文章/页面摘要
  2. function?fanly_excerpt($len=220){
  3. ????if?(?is_single()?||?is_page()?){
  4. ????????global?$post;
  5. ????????if?($post->post_excerpt)?{
  6. ????????????$excerpt??=?$post->post_excerpt;
  7. ????????}?else?{
  8. ????????????if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){
  9. ????????????????$post_content?=?$result['1'];
  10. ????????????}?else?{
  11. ????????????????$post_content_r?=?explode("\n",trim(strip_tags($post->post_content)));
  12. ????????????????$post_content?=?$post_content_r['0'];
  13. ????????????}
  14. ????????????$excerpt?=?preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,0}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$post_content);
  15. ????????}
  16. ????????return?str_replace(array("\r\n",?"\r",?"\n"),?"",?$excerpt);
  17. ????}
  18. }
  19. //优先获取文章中的三张图,否则依次获取自定义图片/特色缩略图/文章首图?last?update?2017/11/23
  20. function?fanly_post_imgs(){
  21. ????global?$post;
  22. ????$content?=?$post->post_content;
  23. ????preg_match_all('/<img?.*?src=[\"|\'](.+?)[\"|\'].*?>/',?$content,?$strResult,?PREG_PATTERN_ORDER);??
  24. ????$n?=?count($strResult[1]);??
  25. ????if($n?>=?3){
  26. ????????$src?=?$strResult[1][0].'","'.$strResult[1][1].'","'.$strResult[1][2];
  27. ????}else{
  28. ????????if(?$values?=?get_post_custom_values("thumb")?)?{???//输出自定义域图片地址
  29. ????????????$values?=?get_post_custom_values("thumb");
  30. ????????????$src?=?$values?[0];
  31. ????????}?elseif(?has_post_thumbnail()?){???//如果有特色缩略图,则输出缩略图地址
  32. ????????????$thumbnail_src?=?wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
  33. ????????????$src?=?$thumbnail_src?[0];
  34. ????????}?else?{????//文章中获取
  35. ????????????if($n?>?0){?//?提取首图
  36. ????????????????$src?=?$strResult[1][0];
  37. ????????????}
  38. ????????}
  39. ????}
  40. ????return?$src;
  41. }

 

三、content.php改造

将如下代码放入/wp-content/themes/begin/template/content.php文件中大概116行后,

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

放置后结果如下:

  1. <?php?if?(zm_get_option('single_weixin'))?{??>
  2. ????<?php?get_template_part(?'template/weixin'?);??>
  3. <?php?}??>
  4. <script>cambrian.render('tail')</script>

最后可以到百度熊掌号改造页中进行结果测试一下是否改造成功或者提交一条新产生的文章url是否成功。

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: