網站內部的 SEO 無非就是控制連結的 nofollow 屬性,圖片新增 alt 標籤,頁面新增關鍵詞 (Keywords) 頁面描述 (Description) 與頁面標題 (title) 這些,有時候單個單個來最佳化的確很麻煩,而使用資料庫批次操作對於新手又太過於複雜,那麼有什麼辦法讓 WordPress 網站自動的來進行 SEO 最佳化呢?下面小編來教大家如何讓 WordPress 網站自動做好站內的 SEO 最佳化。
首先是網站的 Keywords 、 Description 與 title 的最佳化,開啟網站的 header.php 檔案 (PS:有些主題可能不同,但通常情況下是這個檔案) 加入以下程式碼:
<?php
if(is_home()) { ?>
<title> 首頁標題</title>
<meta content="首頁描述" name="Description"/>
<meta content="首頁關鍵詞" name="Keywords"/>
<?php } ?>
<?php
if(is_category()) { ?>
<title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo(name);?><? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('第 %s 頁 ',$paged); ?></title>
<meta content="<?php echo trim(strip_tags(category_description())); ?>" name="Description"/>
<meta content="<?php echo single_cat_title(); ?>" name="Keywords"/>
<?php } ?>
<?php
if(is_single()) { ?>
<title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo(name);?><? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('第 %s 頁 ',$paged); ?></title>
<meta name="description" content="<?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 200,"......","utf-8"); ?>" />
<?php
$keywords = get_the_tags();$keyword = '';
foreach ($keywords as $value) {
$keyword .= ','.$value->name;
}
?>
<meta name="keywords" content="<?php echo $keyword ;?>" />
<?php }?>
<?php
if(is_tag()) { ?>
<title><?php echo trim(wp_title('',0)); ?>_標籤 tag 頁<? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('第 %s 頁 ',$paged); ?></title>
<meta name="description" content="<?php echo trim(strip_tags(tag_description())); ?><? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('第 %s 頁 ',$paged); ?>" />
<meta name="keywords" content="<?php echo trim(wp_title('',0)); ?>" />
<?php }?>
加上以上程式碼後 WordPress 就會對當前頁面自動新增 Keywords 、 Description 與 title 屬性,接下來是為文章中的外鏈新增 nofollow 屬性。
add_filter( 'the_content', 'wxd_seo_wl');
function wxd_seo_wl( $content ) {
$regexp = "<as[^>]*href=("??)([^" >]*?)1[^>]*>";
if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
if( !empty($matches) ) {
$srcUrl = get_option('siteurl');
for ($i=0; $i < count($matches); $i++)
{
$tag = $matches[$i][0];
$tag2 = $matches[$i][0];
$url = $matches[$i][0];
$noFollow = '';
$pattern = '/targets*=s*"s*_blanks*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$noFollow .= ' target="_blank" ';
$pattern = '/rels*=s*"s*[n|d]ofollows*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$noFollow .= ' rel="nofollow" ';
$pos = strpos($url,$srcUrl);
if ($pos === false) {
$tag = rtrim ($tag,'>');
$tag .= $noFollow.'>';
$content = str_replace($tag2,$tag,$content);
}
}
}
}
$content = str_replace(']]>', ']]>', $content);
return $content;
}
將以上程式碼加入到當前主題的 functions.php 檔案即可實現,換主題的時候記得把這段程式碼加到新主題裡噢,不然換主題後文章中的外部連結就會變成無 nofollow 屬性的了。
最後是給圖片新增 alt 標籤:
add_filter('the_content', 'v7v3seo');
function v7v3seo($content) {
global $post;
$pattern ="/<a(.*?)href=('|")(.*?).(bmp|gif|jpeg|jpg|png)('|")(.*?)>/i";
$replacement = '<a$1href=$2$3.$4$5 alt="'.$post->post_title.'" title="'.$post->post_title.'"$6>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
以上程式碼也是加入到 functions.php,好了這樣一來就實現了 WordPress 網站的自動 SEO,接下來就是安心寫原創咯~~
2013.8.13 更新:
多謝網友 @精神寶寶 的提醒程式碼中我把 is_home 寫成了 is_haome,目前已修復!