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