如果我們有需要給WordPress添加Open Graph標(biāo)簽的話,我們可以直接在頭部添加代碼,或者是用 Open Graph 插件,有些SEO插件也是有自帶的。對(duì)于Open Graph標(biāo)簽來說,谷歌等海外的搜索引擎應(yīng)該是比較友好的,看很多外貿(mào)網(wǎng)站有用到,如果我們有谷歌優(yōu)化的外貿(mào)網(wǎng)站建議還是用上,不管是你用代碼還是插件。
實(shí)際上,Open Graph 標(biāo)簽較多部分還是添加在內(nèi)容頁,如果我們有需要的話,可以直接設(shè)定內(nèi)容頁,當(dāng)然也可以用判斷語句設(shè)置home首頁。這里找到一段代碼還是可用的,是國內(nèi)的網(wǎng)友修改的。
1、原版代碼
/** * WordPress OG協(xié)議 來自 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 獲取文章圖片加強(qiáng)版 */ 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; }
這里還算是比較齊全的,只是設(shè)定在內(nèi)容頁。其實(shí)大部分無需要這么多的標(biāo)簽,我們可以精簡一下。
2、精簡版代碼
//WordPress OG 協(xié)議 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 獲取文章圖片加強(qiáng)版 */ 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; }
這里,我只用到幾個(gè)常用的META標(biāo)簽頭部。
評(píng)論