这是在网络上搜集到 关于wordpress 添加og协议的代码。但是好像并不适用爱地狱资源网

/**
* WordPress 星火计划原创保护专用META优化代码(最终版)
*/
add_action('wp_head', 'starfire',0);
if(!function_exists('starfire')){function starfire(){//新增判断,如果是原创文章才加入星火计划META申明global $wpdb;$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;$copy = get_post_meta($post_id , 'author', true);if (is_singular() && empty($copy)) {date_default_timezone_set('PRC');echo '<meta property="og:type" content="article"/><meta property="article:published_time" content="'.get_the_date('c').'"/><meta property="og:release_date" content="'.get_the_date('c').'"/><meta property="article:author" content="';bloginfo('name'); echo '" />'; echo '<meta property="og:author" content="';bloginfo('name');echo '" />'; echo '<meta property="og:url" content="';the_permalink(); echo '"/>';        echo '<meta property="og:title" content="'.trim(wp_title('',0)).' | '; bloginfo('name'); echo '" />';  echo '<meta property="article:published_first" content="';bloginfo('name');echo ',';       the_permalink(); echo '" /><meta property="og:description" content="'.get_the_excerpt().'" /><meta property="og:image" content="'.get_mypost_thumbnail($post_id).'" /><meta itemprop="image" content="' . get_mypost_thumbnail($post_id) . '" />';}}
}
/**
* WordPress 获取文章摘要整理版
*/
function get_mypost_excerpt($post_ID,$len){if (!function_exists('utf8Substr')) {function utf8Substr($str, $from, $len) {return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$str);}}if(!$post_content){$post = get_post($post_ID);$post_content = $post->post_content;}if ($post->post_excerpt) {$description  = $post->post_excerpt;} else {if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){$post_content = $result['1'];} else {$post_content_r = explode("\n",trim(strip_tags($post->post_content)));$post_content = $post_content_r['0'];}$description = utf8Substr($post_content,0,$len);return $description;}
}
/**
* WordPress 获取文章图片加强版
*/
function get_mypost_thumbnail($post_ID){if (has_post_thumbnail()) {$timthumb_src = wp_get_attachment_image_src( get_post_thumbnail_id($post_ID), 'full' ); $url = $timthumb_src[0];} else {if(!$post_content){$post = get_post($post_ID);$post_content = $post->post_content;}preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', do_shortcode($post_content), $matches);if( $matches && isset($matches[1]) && isset($matches[1][0]) ){       $url =  $matches[1][0];}else{$url =  '';}}return $url;
}

于是又找了找
找到了在header.php的代码如下

    <meta property="og:title" content="<?php the_title();?>" />  <meta property="og:site_name " content="<?php bloginfo('name'); ?>" />  <?php if (is_single()) : ?><meta property="og:type" content="acticle" />  <meta property="article:published_time" content="<?php echo get_the_date('c');?>"/><meta property="og:release_date" content="<?php echo get_the_date('c'); ?> "/><meta property="article:author" content="<?php echo bloginfo('name'); ?> " />';<meta property="og:author" content="'<?php echo bloginfo('name'); ?>" />';<meta property="article:published_first" content="<?php bloginfo('name'); ?>,<?php the_permalink();?>" /><meta property="og:description" content="<?php  echo get_the_excerpt(); ?>" /><meta property="og:url" content="<?php the_permalink();?>"/>  <?php if ( has_post_thumbnail()):   $meta_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail'); ?>
<meta property="og:image" content="<?php echo $meta_image_url[0];?>" />  <?php endif; ?>  <?php else: ?>  <meta property="og:type" content="website" />
<?php endif; ?>

修改其中的

<meta property="og:description" content="<?php echo get_post_excerpt(); ?>" />  

<meta property="og:description" content="<?php echo get_the_excerpt(); ?>" />  

最终我的解决方案是通过 Code Snippets 插件

添加代码 方便统一管理 代码如下 当然,你可以自己修改成需要的样式

/**
* WordPress og协议 爱地狱自用版
*/
add_action('wp_head', 'starfire',0);
if(!function_exists('starfire')){function starfire(){//新增判断,如果是原创文章才加入星火计划META申明global $wpdb;$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;$copy = get_post_meta($post_id , 'author', true);if (is_singular() && empty($copy)) {date_default_timezone_set('PRC');echo '<meta property="og:type" content="article"/><meta property="article:published_time" content="'.get_the_date('c').'"/><meta property="og:release_date" content="'.get_the_date('c').'"/><meta property="article:author" content="';bloginfo('name'); echo '" />'; echo '<meta property="og:author" content="';bloginfo('name');echo '" />'; echo '<meta property="og:url" content="';the_permalink(); echo '"/>';        echo '<meta property="og:title" content="'.trim(wp_title('',0)).' | '; bloginfo('name'); echo '" />';  echo '<meta property="article:published_first" content="';bloginfo('name');echo ',';       the_permalink(); echo '" /><meta property="og:description" content="'.get_the_excerpt().'" /><meta property="og:image" content="'.get_mypost_thumbnail($post_id).'" /><meta itemprop="image" content="' . get_mypost_thumbnail($post_id) . '" />';}}
}
/**
* WordPress 获取文章摘要整理版
*/
function get_mypost_excerpt($post_ID,$len){if (!function_exists('utf8Substr')) {function utf8Substr($str, $from, $len) {return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$str);}}if(!$post_content){$post = get_post($post_ID);$post_content = $post->post_content;}if ($post->post_excerpt) {$description  = $post->post_excerpt;} else {if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){$post_content = $result['1'];} else {$post_content_r = explode("\n",trim(strip_tags($post->post_content)));$post_content = $post_content_r['0'];}$description = utf8Substr($post_content,0,$len);return $description;}
}
/**
* WordPress 获取文章图片加强版
*/
function get_mypost_thumbnail($post_ID){if (has_post_thumbnail()) {$timthumb_src = wp_get_attachment_image_src( get_post_thumbnail_id($post_ID), 'full' ); $url = $timthumb_src[0];} else {if(!$post_content){$post = get_post($post_ID);$post_content = $post->post_content;}preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', do_shortcode($post_content), $matches);if( $matches && isset($matches[1]) && isset($matches[1][0]) ){       $url =  $matches[1][0];}else{$url =  '';}}return $url;
}

原文地址: https://www.aidiyu.com/812.html

简单步骤实现wordpress添加og协议相关推荐

  1. WordPress博客文章页如何添加OG协议标签

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

  2. wordpress克隆站点_如何通过7个简单步骤克隆WordPress网站

    wordpress克隆站点 Generally speaking, cloning is considered an unethical practice. However, when you thi ...

  3. 如何给网站添加 OG 协议

    示例参考: <meta property="og:site_name" content="Colby Fayock" /> <metaprop ...

  4. og协议-有利于SNS网站分享

    一丶前言 全球快递toWhere官网发现使用og协议,并且支付宝和淘宝活动源码也会添加og协议,查阅资料弄清og协议是什么,此刻附上og协议官方文档 一丶什么是og协议 Open Graph通讯协定( ...

  5. 增加seo好感度,wordpress给文章增加og协议,即meta标签形式如 meta property=”og:

    这个标题是什么意思?什么是og协议.先不说到底是什么意思.可能大家在一些博客上面,有时候一篇文章,你查看源代码的时候,会在里面,发现如"meta property="og:desc ...

  6. 网站添加头部Meta property=og协议的使用方法详解教程

    Meta Property=og怎么使用? 如何调用og:type?怎么在文章中调用og:title?如何使用调取文章og:image?本文将介绍什么是Meta Property=og协议以及该协议的 ...

  7. wordpress添加媒体_4种将音频添加到WordPress网站的简单方法

    wordpress添加媒体 This article is part of a series created in partnership with SiteGround. Thank you for ...

  8. 16.WireShark学习-在WireShark中添加新协议

    16. 在Wireshark中添加新协议 WireShark编程基础 使用Lua开发简单扩展功能 使用WireShark开发新的协议解析器 测试新协议 WireShark支持Lua语言编写的脚本 16 ...

  9. 如何在Wordpress添加谷歌地图,外贸网站插入谷歌地图的方法

    如何在Wordpress添加谷歌地图,外贸网站插入谷歌地图的方法 前言 在wordpress网站里添加google map是很多外贸建站的小伙伴总会遇到的事情.本文简单介绍下怎么不用写代码也能在网站里 ...

最新文章

  1. Github新建分支以处理原仓库提交时detached HEAD的问题
  2. volatile修饰的变量_看了这篇volatile详细介绍,面试你会害怕?
  3. 使用Numpy和Opencv完成图像的基本数据分析(Part II)
  4. css透明度的设置 (兼容所有浏览器)
  5. eve星战前夜登录提示服务器维护中,EVE星战前夜进不去怎么办 游戏进不去问题解决方法...
  6. Android自定义processor实现bindView功能
  7. adb shell 操作
  8. 金融级云服务 平安云赋能保险业创新发展
  9. 英式音标和美式音标的差异
  10. window10系统ie设置代理,保存不了的问题
  11. 计算机毕业设计Java自行车在线租赁管理系统2021(源码+系统+mysql数据库+Lw文档)
  12. java实现word、pdf文件下载功能
  13. windows用ffmpeg将flv视频转换为mp4
  14. 树莓派11bullseye换源/Opencv安装
  15. Error: Failed to load config “standard“ to extend from
  16. c++中二维数组与二维向量的长度
  17. Go Moudle笔记
  18. 关于卷积神经网络,了解一下
  19. python音标1003python音标_python selenium 爬取百度翻译单词音标
  20. 01- 初识MySQL

热门文章

  1. 大数据行业再拉警报,51信用卡或将彻底沦陷?
  2. Java kafka监控 topic的数据量count情况,每个topic的Summed Recent Offsets(总结最近的偏移量)
  3. interlib android客户端开发,基于Android的移动图书馆设计与开发
  4. oss :Request has expired.
  5. 51nod1431 快乐排队
  6. 支持免费试用的香港云服务器推荐
  7. vue项目访问的时候,用localhost能访问,但是用本机ip就不能访问 的解决办法,亲测有效
  8. 技术管理者应具备哪些能力
  9. RenderSystem_Direct3D9_d 不能加载
  10. Direct3D 12 尝鲜(二): Fence