網(wǎng)站內(nèi)部的SEO無非就是控制鏈接的nofollow屬性,圖片添加alt標簽,,頁面添加關鍵詞(Keywords)頁面描述(Description)與頁面標題(title)這些,,有時候單個單個來優(yōu)化的確很麻煩,而使用數(shù)據(jù)庫批量操作對于新手又太過于復雜,,那么有什么辦法讓wordpress網(wǎng)站自動的來進行SEO優(yōu)化呢,?下面來教大家如何讓wordpress網(wǎng)站自動做好站內(nèi)的SEO優(yōu)化。
首先是網(wǎng)站的Keywords,、Description與title的優(yōu)化,,打開網(wǎng)站的header.php文件(PS:有些主題可能不同,但通常情況下是這個文件)加入以下代碼:
<?phpif(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', 'zyku_seo_wl');function zyku_seo_wl( $content ) { $regexp = "<a\s[^>]*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 = '/target\s*=\s*"\s*_blank\s*"/'; preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE); if( count($match) < 1 ) $noFollow .= ' target="_blank" '; $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/'; 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文件即可實現(xiàn),,換主題的時候記得把這段代碼加到新主題里噢,,不然換主題后文章中的外部鏈接就會變成無nofollow屬性的了。
最后是給圖片添加alt標簽:
add_filter('the_content', 'zykuseo');function zykuseo($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,,好了這樣一來就實現(xiàn)了wordpress網(wǎng)站的自動SEO優(yōu)化了,。