让wordpress网站全自动SEO优化的方法

网站内部的seo无非就是控制链接的nofollow属性,图片添加alt标签,页面添加关键词(Keywords)页面描述(Description)与页面标题(title)这些,有时候单个单个来优化的确很麻烦,而使用数据库批量操作对于新手又太过于复杂,那么有什么办法让wordpress网站自动的来进行SEO优化呢?下面小V来教大家如何让wordpress网站自动做好站内的SEO优化。

首先是网站的Keywords、Description与title的优化,打开网站的header.php文件(PS:有些主题可能不同,但通常情况下是这个文件)加入以下代码:

<?php
if(is_haome()) { ?>
<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', 'auto_nofollow');
               
function auto_nofollow($content) {
               
    return preg_replace_callback('/<a>]+/', '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("%(S(?!$site_link))%i",  'rel="nofollow" $1', $link);
    } elseif (preg_match("%S(?!$site_link)%i",  $link)) {
        $link = preg_replace('/rel=S(?!nofollow)S*/i', 'rel="nofollow"', $link);
    }
    return $link;
}

将以上代码加入到当前主题的functions.php文件即可实现,换主题的时候记得把这段代码加到新主题里噢,不然换主题后文章中的外部链接就会变成无nofollow属性的了。

原文链接:http://v7v3.com/wpjiaocheng/201307198.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章