来源: 互联网 日期: 2020-07-16 17:08:43
网站内部的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.zhuarun.com)