简单代码给WordPress内容页添加Open Graph标签

老乐 定制开发2字数 4504阅读15分0秒阅读模式

如果我们有需要给WordPress添加Open Graph标签的话,我们可以直接在头部添加代码,或者是用 Open Graph 插件,有些SEO插件也是有自带的。对于Open Graph标签来说,谷歌等海外的搜索引擎应该是比较友好的,看很多外贸网站有用到,如果我们有谷歌优化的外贸网站建议还是用上,不管是你用代码还是插件。

实际上,Open Graph 标签较多部分还是添加在内容页,如果我们有需要的话,可以直接设定内容页,当然也可以用判断语句设置home首页。这里找到一段代码还是可用的,是国内的网友修改的。

1、原版代码

/**
* WordPress OG协议 来自 aidiyu
*/
add_action('wp_head', 'starfire',0);
if(!function_exists('starfire')){
function starfire(){

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;
}

这里还算是比较齐全的,只是设定在内容页。其实大部分无需要这么多的标签,我们可以精简一下。

2、精简版代码

//WordPress OG 协议 cnwper.com 精简版
add_action('wp_head', 'starfire',0);
function starfire(){
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="og:site_name" content="';bloginfo('name'); echo '" />'; echo '
<meta property="article:published_time" content="'.get_the_date('c').'"/>
<meta property="og:url" content="';the_permalink(); echo '"/>'; echo '
<meta property="og:title" content="'.trim(wp_title('',0)).' - '; bloginfo('name'); echo '" />'; echo '
<meta property="og:description" content="'.get_the_excerpt().'" />
<meta property="og: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;
}

这里,我只用到几个常用的META标签头部。

投上你的一票
 
  • 本文由 老乐 发表于 2024年11月14日 21:24:12
  • 转载请务必保留本文链接:https://www.zhujipingjia.com/wp-open-graph.html
  • Open Graph标签