0 收藏 0 点赞 987 浏览 3492 个字

我们已经知道开放内容(OG)协议对SEO优化的作用,而在日常应用中,我们也能经常看到一些WordPress博客会在自己的页面的部分加入OG协议,实现起来很容易。

这个协议有什么好处?据说这个东西是标明原创的一个要素。经过小白几个网站的测试。使用这个og协议的网站,明显百度要更青睐。收录更快。这是我亲测得到的结果。所以,我准备把其他几个没有og协议的站点也改进一下。

今天觅站网就说说如何为WordPress博客文章页添加OG协议标签。因为WordPress大部分为文章形式页面,所以我们只需要添加以下几个协议即可:

  1. /** 
  2. * WordPress 星火计划原创保护专用META优化代码(最终版)  
  3. */  
  4. add_action('wp_head', 'starfire',0);  
  5. if(!function_exists('starfire')){  
  6.   function starfire(){  
  7.     //新增判断,如果是原创文章才加入星火计划META申明  
  8.      global $wpdb;  
  9.      $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;  
  10.      $copy = get_post_meta($post_id , 'author', true);  
  11.      if (is_singular() && empty($copy)) {  
  12.         date_default_timezone_set('PRC');  
  13.         echo '<meta property="og:type" content="article"/>  
  14.         <meta property="article:published_time" content="'.get_the_date('c').'"/>  
  15.         <meta property="og:release_date" content="'.get_the_date('c').'"/>  
  16.         <meta property="article:author" content="';bloginfo('name'); echo '" />';  
  17.         echo '<meta property="og:author" content="';bloginfo('name');echo '" />';  
  18.         echo '<meta property="og:url" content="';the_permalink(); echo '"/>';  
  19.         //输出文章标题+分隔符+网站名称,不喜欢这种形式的请自行改造(如果不需要这个标签,请删除以下三行)。  
  20.         echo '<meta property="og:title" content="'.trim(wp_title('',0)).' | '; bloginfo('name'); echo '" />';  
  21.         //输出博客名称,如果想改成其他内容,比如作者请自行修改 bloginfo('name')  
  22.         echo '<meta property="article:published_first" content="';bloginfo('name');echo ','; 
  23.         the_permalink(); 
  24.         //默认截取文章220个字作为摘要,可以自行修改下行220为其他整数 
  25.         echo '" /><meta property="og:description" content="'.get_mypost_excerpt($post_id, 220).'……" />  
  26.         <meta property="og:image" content="'.get_mypost_thumbnail($post_id).'" />  
  27.         <meta itemprop="image" content="' . get_mypost_thumbnail($post_id) . '" />';  
  28.     }  
  29.   }  
  30. }  
  31. /** 
  32. * WordPress 获取文章摘要整理版  
  33. */  
  34. function get_mypost_excerpt($post_ID,$len){  
  35.     if (!function_exists('utf8Substr')) {  
  36.         function utf8Substr($str, $from, $len) {  
  37.             return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.  
  38.                 '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',  
  39.                 '$1',$str);  
  40.         }  
  41.     }  
  42.     if(!$post_content){  
  43.             $post = get_post($post_ID);  
  44.             $post_content = $post->post_content;  
  45.    }  
  46.     if ($post->post_excerpt) {  
  47.             $description  = $post->post_excerpt;  
  48.     } else {  
  49.         if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){  
  50.             $post_content = $result['1'];  
  51.         } else {  
  52.             $post_content_r = explode("\n",trim(strip_tags($post->post_content)));  
  53.             $post_content = $post_content_r['0'];  
  54.         }  
  55.         $description = utf8Substr($post_content,0,$len);  
  56.         return $description;  
  57.     }  
  58. }  
  59. /** 
  60. * WordPress 获取文章图片加强版  
  61. */  
  62. function get_mypost_thumbnail($post_ID){  
  63.     if (has_post_thumbnail()) {  
  64.             $timthumb_src = wp_get_attachment_image_src( get_post_thumbnail_id($post_ID), 'full' );   
  65.             $url = $timthumb_src[0];  
  66.     } else {  
  67.         if(!$post_content){  
  68.             $post = get_post($post_ID);  
  69.             $post_content = $post->post_content;  
  70.         }  
  71.         preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', do_shortcode($post_content), $matches);  
  72.         if( $matches && isset($matches[1]) && isset($matches[1][0]) ){         
  73.             $url =  $matches[1][0];  
  74.         }else{  
  75.             $url =  '';  
  76.         }  
  77.     }  
  78.     return $url;  
  79. }  

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.mizhanw.com/blog/1750.html

相关推荐
最佳WordPress安全插件推荐清单
仅最初的网站投资就足以从一开始就保护您的网站。黑客、恶意软件、后门攻击和SEO垃圾邮件只是等待利用您的服务器、访问者数据和网站基础设施的挥之…
日期:2023-05-26 点赞:0 阅读:472
WordPress网站安全管理防火墙插件 – All In One WP Security & Firewall
WordPress网站安全管理防火墙插件 – All In One WP Security & Firewall 是一款非常优秀的W…
日期:2023-05-26 点赞:0 阅读:617
WordPress网站搬家与换域名详细教程
WordPress网站搬家是WP站长都会遇到的问题,wordpress怎么搬家对于新手而言可能是一头雾水,不知道该做哪些,网上找到的相关教程…
日期:2023-05-25 点赞:0 阅读:1,750
wordpress文章页获取作者的标签,the_author(); 不显示怎么办?
wordpress里面可自定义的东西太多了,没有一个统一的标准,wordpress版本有多,版本之间的调用方法又有差异,你在网上搜到的教程可…
日期:2023-05-23 点赞:0 阅读:367
wordpress安装教程 每一个步骤都带图示例
wordpress的安装还是比较简单的,本文来用一步骤一图的比较直观的方式来给大家介绍wordpress安装步骤。 首先要先下载wordpr…
日期:2023-05-23 点赞:0 阅读:581
wordpress调用指定ID文章的代码
在折腾wordpress模板的时候遇到“调用指定ID文章”的需求,折腾一番成功后分享下: 上面这段代码中的“$options['theme_…
日期:2023-05-23 点赞:0 阅读:144
发表评论
暂无评论

还没有评论呢,快来抢沙发~