不論是什麼類型的網站或博客,搜尋引擎(Google、Yahoo、百度)的蜘蛛抓取內容,都先掃瞄Meta Name的 Description 和 Keywords 相關描述及標籤,以便加入索引清單。或是訪客轉貼文章時,擷取的內容也會依照 Description 所描述的為依照。
同時!WordPress 加上 Description 和 Keywords 標簽有助提升 SEO 的友好。
在主題的 header.php 文件加入以下代碼▼
版本一:
簡介:Description 自動擷取內文的前 180 個文字。其中 「180」可以自由改動,一般設置大概在 120 ~ 200 個字左右為佳。
<meta name="keywords" content="tag1,tag2,tag3" />
<meta name="description" content="<?php if (is_single() || is_page()) {echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 180,"…");} else{echo ("不在文章內容頁和專頁(頁面)的描述");} ?>" />
版本二:
簡介:Description 標籤只出現在首頁、分類頁和標籤頁;Single文章頁及Pages專頁(頁面)會被隱藏。你需要進入控制台(後台)添加分類及標籤的內容說明。
<meta name="keywords" content="tag1,tag2,tag3" />
<?php if ( is_home() ) { ?>
<meta name="description" content="主頁的描述" />
<?php } elseif(is_category() || is_tag() ) { ?>
<meta name="description" content="<?php echo trim(strip_tags(category_description())); ?>" />
<?php } ?>
版本三:
簡介:Description 標籤只出現在首頁、文章頁和專頁(頁面);其他頁面一律不顯示。
<meta name="keywords" content="tag1,tag2,tag3" />
<?php if ( is_home() ) { ?>
<meta name="description" content="主頁的描述" />
<?php } elseif(is_single() || is_page() ) { ?>
<meta name="description" content="<?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 180,"…"); ?>" />
<?php } ?>
其中 <meta name="keywords" content="tag1,tag2,tag3" /> 說明:
1. 如果想自動抓取「標題」為關鍵字。
修改為:二選一
<meta name="keywords" content="<?php if (is_home()) { bloginfo('name'); } else { echo trim(wp_title('',0)); } ?>" />
<meta name="keywords" content="<?php echo wp_get_document_title(); ?>" />
2. 如果想調用「Tags」作 keywords 關鍵詞為標籤。
修改為:
<meta name="keywords" content="<?php $tags = get_tags();
$html = '';
foreach ( $tags as $tag ) {
$html .= "{$tag->name},";
}
$html .= '';
echo $html; ?>" />
其他版本:WordPress獨立的Description和Keywords
DEMO: 請看圖片
備用代碼:
<?php if ( is_category() ) { ?>
<!--分類meta Start-->
<meta name="keywords" content="<?php echo single_cat_title(); ?>,其它網站關鍵字1,其它網站關鍵字2" />
<meta name="description" content="網站名稱。<?php echo single_cat_title(); ?>" />
<!--分類meta End-->
<?php } ?>
<?php if ( is_home() ) { ?>
<!--首頁meta Start-->
<meta name="keywords" content="首頁關鍵字1,首頁關鍵字2,首頁關鍵字3," />
<meta name="description" content="首頁 Description,建議40至80字內" />
<!--首頁meta End-->
<?php } ?>
<?php if (is_single() || is_page() ) { ?>
<meta name="keywords" content="<?php $key="keywords"; echo get_post_meta($post->ID, $key, true); ?>" />
<meta name="description" content="<?php $key="description"; echo get_post_meta($post->ID, $key, true); ?>" />
<meta property="og:image" content="<?php $key="fb-img"; echo get_post_meta($post->ID, $key, true); ?>" />
<meta property="og:title" content="<?php $key="fb-title"; echo get_post_meta($post->ID, $key, true); ?>" />
<meta property="og:url" content="<?php $key="fb-url"; echo get_post_meta($post->ID, $key, true); ?>" />
<meta property="og:description" content="<?php $key="fb-description"; echo get_post_meta($post->ID, $key, true); ?>" />
<?php } ?>
<!--所有頁面通用標籤Start-->
<meta property="og:type" content="article" />
<meta property="og:locale" content="zh_TW" />
<meta property="og:site_name" content="網站名稱" />
<meta name="copyright" content="網站版權歸屬"/>
<meta name="Author" content="作者" />
<!--所有頁面通用標籤End-->