欢迎来到资源库(www.zyku.net)

WordPress

当前位置:首页 > CMS教程 > WordPress > SEO

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

时间:2017-03-04|栏目:WordPress|点击:|我要投稿

网站内部的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', '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文件即可实现,换主题的时候记得把这段代码加到新主题里噢,不然换主题后文章中的外部链接就会变成无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,好了这样一来就实现了wordpress网站的自动SEO优化了。

(资源库 www.zyku.net)

上一篇:wordpress复制文章内容自动添加版权的方法

栏    目:WordPress

下一篇:wordpress最常用SQL查询语句大集合

本文标题:wordpress网站全自动SEO优化的方法

本文地址:https://www.zyku.net/wp/1096.html

关于我们 | 版权申明 | 寻求合作 |

重要申明:本站所有的文章、图片、评论等内容,均由网友发表或上传并维护或收集自网络,仅供个人学习交流使用,版权归原作者所有。

如有侵犯您的版权,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:95148658 | 邮箱:mb8#qq.com(#换成@)

苏ICP备2020066115号-1

本网站由提供CDN加速/云存储服务