告所有本站开发WP主题的强化SEO方案

我们已经知道开放内容(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. }  

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有M币奖励和额外收入!

模板下载吧 主题使用教程 告所有本站开发WP主题的强化SEO方案 https://www.mbxzb.com/blog/44730.html

从明天起,做一个幸福的人,喂马、劈柴、周游世界…

常见问题
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用
查看详情

相关文章

评论
暂无评论
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 威武 友军 顶贴
官方客服团队

为您解决烦忧 - 24小时在线 专业服务