| 
 | 
 
如何来做好WordPress网站的站内优化呢? 
建立面包屑导航将是最好的优化方案。 
一、Wordpress网站架构层级:面包屑导航 
1、打开Wordpress的functions.php文件并加入以下代码。 
function dimox_breadcrumbs() { 
$delimiter = '»'; 
$name = '首页'; 
$currentBefore = ''; 
$currentAfter = ''; 
if ( !is_home() && !is_front_page() || is_paged() ) { 
global $post; 
$home = get_bloginfo('url'); 
echo '[url=]' . $name . '[/url] ' . $delimiter . ' '; 
if ( is_category() ) { 
global $wp_query; 
$cat_obj = $wp_query->get_queried_object(); 
$thisCat = $cat_obj->term_id; 
$thisCat = get_category($thisCat); 
$parentCat = get_category($thisCat->parent); 
if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' ')); 
echo $currentBefore . '当前分类 ''; 
single_cat_title(); 
echo ''' . $currentAfter; 
} elseif ( is_day() ) { 
echo '[url=]' . get_the_time('Y') . '[/url] ' . $delimiter . ' '; 
echo '[url=]' . get_the_time('F') . '[/url] ' . $delimiter . ' '; 
echo $currentBefore . get_the_time('d') . $currentAfter; 
} elseif ( is_month() ) { 
echo '[url=]' . get_the_time('Y') . '[/url] ' . $delimiter . ' '; 
echo $currentBefore . get_the_time('F') . $currentAfter; 
} elseif ( is_year() ) { 
echo $currentBefore . get_the_time('Y') . $currentAfter; 
} elseif ( is_single() ) { 
$cat = get_the_category(); $cat = $cat[0]; 
echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' '); 
echo $currentBefore; 
the_title(); 
echo $currentAfter; 
} elseif ( is_page() && !$post->post_parent ) { 
echo $currentBefore; 
the_title(); 
echo $currentAfter; 
} elseif ( is_page() && $post->post_parent ) { 
$parent_id = $post->post_parent; 
$breadcrumbs = array(); 
while ($parent_id) { 
$page = get_page($parent_id); 
$breadcrumbs[] = '[url=]ID) . '">'. get_the_title($page->ID) . '[/url]'; 
$parent_id = $page->post_parent; 
} 
$breadcrumbs = array_reverse($breadcrumbs); 
foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter .' '; 
echo $currentBefore; 
the_title(); 
echo $currentAfter; 
} elseif ( is_search() ) { 
echo $currentBefore . 'Search results for '' . get_search_query() . ''' . $currentAfter; 
} elseif ( is_tag() ) { 
echo $currentBefore . '当前标签页 ''; 
single_tag_title(); 
echo ''' . $currentAfter; 
} elseif ( is_author() ) { 
global $author; 
$userdata = get_userdata($author); 
echo $currentBefore . '当前文章页 ' . $userdata->display_name .$currentAfter; 
} elseif ( is_404() ) { 
echo $currentBefore . 'Error 404' . $currentAfter; 
} 
if ( get_query_var('paged') ) { 
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' ('; 
echo __('Page') . ' ' . get_query_var('paged'); 
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')'; 
} 
} 
} 
2、然后在需要显示面包屑导航的地方加入以下代码。 
 
3、好了这样就完成了WordPress SEO的面包屑导航,接下来我们做的就是要控制站内导出链接页面所输出的权重。 
二、Wordpress防止权重流失:自动添加nofollow属性 
1、大家在写文章时引用的站外链接大多数都是参考文献之类的,我们可以给这些链接添加nofollow属性来减少不必要的权重流失。 
2、手动添加nofollow属性的话可以精准的控制链接的权重导出,但是太过于麻烦,而SQL批量操作对于新手又太难。 
3、下面小V来教大家如何给wordpress网站文章中所导出的外链自动添加nofollow属性。 将以下代码加入到当前主题的functions.php文件即可实现。 
add_filter('the_content', 'auto_nofollow'); 
function auto_nofollow($content) { 
return preg_replace_callback('/]+/', 'auto_nofollow_callback',$content); 
} 
function auto_nofollow_callback($matches) { 
$link = $matches[0]; 
$site_link = get_bloginfo('url'); 
if (strpos($link, 'rel') === false) { 
$link = preg_replace("%(href=S(?!$site_link))%i",'rel="nofollow" $1', $link); 
} elseif (preg_match("%href=S(?!$site_link)%i", $link)) { 
$link = preg_replace('/rel=S(?!nofollow)S*/i','rel="nofollow"', $link); 
} 
return $link; 
} 
4、换主题的时候记得把这段代码加到新主题里噢,不然换主题后文章中的外部链接就会变成无nofollow属性的了。 
三、Wordpress集中权重:指定分类不输出权重 
1、很多wordpress的个人博主在建立博客后会经常写一些生活中的琐碎小事,但是那些文章是完全不会给网站从搜索引擎带来流量的。 
2、如果这些文章在站内展示率或出现率太高这样就会把需要权重的页面的权重给分散了。为了不让权重分散我们就只好用robots.txt来屏蔽该目录下的文章了。 
3、有些博主不希望这类页面把站点的权重分散了,但是又想让搜索引擎收录这些页面那该怎么办呢?这样我们可以用nofollow属性来控制页面间权重的传递。 
function nofollow_cat_posts($text) { 
global $post; 
if( in_category(1) ) { // 将1替换成你要输出nofollow链接的目录id 
$text = stripslashes(wp_rel_nofollow($text)); 
} 
return $text; 
} 
add_filter('the_content', 'nofollow_cat_posts'); 
4、将以上代码加入到你当前使用的主题的functions.php里则可以指定分类链接在输出时带nofollow属性,从而达到集中网站权重的效果。 
四、Wordpress临时维护时的SEO处理 
1、不管任何网站都随时可能出现问题,那么当wordpress网站或博客在遇见问题或者是维护时我们该怎么做呢? 
2、直接关闭站点那是非常不明智的方法,对访客或者是搜索引擎来说都不是友好的,你可能会想到直接做个一个维护提示页面然后将网站请求全部都定向到维护提示页面。 
3、这样处理虽然对与用户体验来说是非常友好的但是对于搜索引擎来说可就糟糕透顶了,在搜索引擎看来你整个网站的内容都变成一样的了,那不K你才奇怪了。 
4、百度站长平台曾推荐过在网站维护时可以返回http 503状态码这样可以告诉搜索引擎,网站正在维护中。 
5、使用wordpress搭建的网站要做到这一点是非常简单的仅仅需要向functions.php文件中添加一些代码就可以。 
function wp_maintenance_mode(){ 
if(!current_user_can('edit_themes') || !is_user_logged_in()){ 
wp_die('网站维护中……', '请在7:00之后再来', array('response' =>'503')); 
} 
} 
add_action('get_header', 'wp_maintenance_mode'); 
6、当网站维护完毕后只需要将代码注释或者删除掉就好了。 
五、Wordpress标签自动描文本 
1、描文本在网站的seo优化中占据着很大的比重,合理的关键词描文本可以给网站的排名带来很大的帮助。 
2、大多数建站系统在布局文章关键的描文本时都要手动去更改、去更新。这样的工作量是非常庞大的,而且效率极其低下。 
3、而有些聪明的站长可能会利用执行数据库语言来批量的更换关键词的描文本,但是对于菜鸟来说数据库操作可能太难,所以小V这里介绍个wordpress自动描文本的方法: 
$match_num_from = 1; //每篇文章中的关键词数量低于多少则不描文本(请不要低于1) 
$match_num_to = 1; //同一篇文章中,同一个关键词最多描几次文本(这里是1次,建议不超过2次) 
add_filter('the_content','tag_link',1); 
function tag_sort($a, $b){ 
if ( $a->name == $b->name ) return 0; 
return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1; 
} 
function tag_link($content){ 
global $match_num_from,$match_num_to; 
$posttags = get_the_tags(); 
if ($posttags) { 
usort($posttags, "tag_sort"); 
foreach($posttags as $tag) { 
$link = get_tag_link($tag->term_id); 
$keyword = $tag->name; 
$cleankeyword = stripslashes($keyword); 
$url = "[url=] 
$url .= ' target="_blank" class="tag_link"'; 
$url .= ">".addcslashes($cleankeyword, '$')."[/url]"; 
$limit = rand($match_num_from,$match_num_to); 
$content = preg_replace( '|(]+>)(.*)('.$ex_word.')(.*)(]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content); 
$content = preg_replace( '|()|U'.$case, '$1$2%&&&&&%$4$5', $content); 
$cleankeyword = preg_quote($cleankeyword,'\''); 
$regEx = '\'(?!((]*?)>)|([^>]*?))\'s' . $case; 
$content = preg_replace($regEx,$url,$content,$limit); 
$content = str_replace( '%&&&&&%', stripslashes($ex_word),$content); 
} 
} 
return $content; 
} 
4、此段代码只会在已经存在的tag上面文本,如果需要自定义其他描文本请使用WP keyword Link Plugin插件。 
六、写在最后:关于Wordpress SEO 
1、有些比较注重SEO的站长,为了降低网站垃圾页面的收录量经常使用robots.txt来屏蔽网站内容。有些站长直接一句Disallow: /xxx/xx 了事,其实这是非常不明智的! 
2、因为站内仍有页面链接向这些垃圾页面,如果直接用robots.txt屏蔽的话会造成这些垃圾页面成为只导入链接而不导出链接的站内权重黑洞,会造成网站权重的流失。 
 
 
文章出自:v7v3 亚峰网络 |   
 
 
 
 |