首先,向大家道歉,在为wordpress网站添加网站关键词及描述代码的文章里,写的那个网站关键词代码及教程我没有经过测试,就发表出来了,对大家很不负责。
所以今天找了一个比较简单的、通俗的,经过提供seo亲自测试过并且还在用的代码,网上很多朋友也在用,所以请大家放心使用。使用这个代码的好处就是免去了插件的累赘,而且插件多了对网站的速度及各方面都不太好。
代码一般插在header.php文件的头部也就是</head>之前,大家也可以按照自己的兴趣来,以下为代码:
<?if (is_home()){
$description = "首页描述";
$keywords = "关键词A,关键词B,关键词C";
} elseif (is_single()){
if ($post->post_excerpt) {
$description = $post->post_excerpt;
} else {
$description = substr(strip_tags($post->post_content),0,220);
}
$keywords = "";
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag ) {
$keywords = $keywords . $tag->name . ", ";
}
}
?>
<meta name="keywords" content="<?=$keywords?>" />
<meta name="description" content="<?=$description?>" />
不过提供网站优化用的是这段代码,它在关键词里添加了最新文章的文章标题,同时将关键词数量改为了150字,还有就是将substr改为了mb_substr,有利于php中文截断,大家按照自己的兴趣来吧,喜欢哪个就用哪个:
<?php if (is_home()){
$description = "首页描述";
$keywords = "首页关键词!";
} elseif (is_single()){
if ($post->post_excerpt) {
$description = $post->post_excerpt;
} else {
$description = mb_substr(strip_tags($post->post_content),0,150);
}$keywords = "";
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag ) {
$keywords = $keywords.$tag->name. ",";
}
}
?>
<meta name="keywords" content="<?php echo $keywords ?><?php the_title(); ?>" />
<meta name="description" content="<?php echo $description ?>" />
忘了说一句,此代码用于wordpress搭建的网站,其他PHP网站没试过,大家有兴趣的话可以试一试。