添加代码实现WordPress熊掌号移动端页面改造

SEO工具1,453

WordPress熊掌号移动端页面改造最重要的其实就是添加 JSON_LD 数据了,其实可以用下面的代码来实现,大家把以下这段代码添加到你需要接入熊掌号的主题的 functions.php 文件中。

  1. //获取文章/页面摘要
  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 2018/01/22
  20. function fanly_post_imgs(){
  21. global $post;
  22. $src = '';
  23. $content = $post->post_content;
  24. preg_match_all('/<img .*?src=[\"|\'](.+?)[\"|\'].*?>/', $content, $strResult, PREG_PATTERN_ORDER);
  25. $n = count($strResult[1]);
  26. if($n >= 3){
  27. $src = $strResult[1][0].'","'.$strResult[1][1].'","'.$strResult[1][2];
  28. }elseif($n >= 1){
  29. $src = $strResult[1][0];
  30. }
  31. return $src;
  32. }
  33.  
  34. //熊掌号h5页面改造
  35. function fanly_h5(){
  36. if(is_single()){
  37. echo '<link rel="canonical" href="'.get_the_permalink().'" />'; 
  38. echo '<script type="application/ld+json">{
  39. "@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld",
  40. "@id": "'.get_the_permalink().'",
  41. "appid": "自己的熊掌号ID",
  42. "title": "'.get_the_title().'",
  43. "images": ["'.fanly_post_imgs().'"],
  44. "description": "'.fanly_excerpt().'",
  45. "pubDate": "'.get_the_time('Y-m-d\TH:i:s').'"
  46. }</script>
  47. ';
  48. echo '<script src="//msite.baidu.com/sdk/c.js?appid=自己的熊掌号ID"></script>';
  49. }
  50. }

其中的【自己的熊掌号ID】,要改成自己网站的熊掌号ID。文章源自SEO视频网-https://www.seoshipin.cn/gongju/2788.html

最后大家在header.php中找到</head>标签,在</head>标签前面加上<?php fanly_h5(); ?>文章源自SEO视频网-https://www.seoshipin.cn/gongju/2788.html

  1. <?php fanly_h5(); ?>

以上代码还做了一个 if 判断,只让该段代码在文章中输出。文章源自SEO视频网-https://www.seoshipin.cn/gongju/2788.html 文章源自SEO视频网-https://www.seoshipin.cn/gongju/2788.html

 
  • 本文由 潮涌SEO 发表于 2019年12月22日 01:39:56
  • 转载请务必保留本文链接:https://www.seoshipin.cn/gongju/2788.html