简单步骤实现wordpress添加og协议

这是在网络上搜集到 关于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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值