WordPress存在一个问题,就是页面中keywords和description信息的缺失,其实这个工作应该由主题来完成,遗憾的是大部分主题并没有涉及,因此造成这两个信息的缺失,对于SEO很不利。下面说下如何用非插件的方式在模板中实现keywords和description的调用。直接上代码吧,下面是天涯PHP博客[http://blog.phpha.com]的主题中的代码,自己根据实际情况就行修改吧,有不明白的可以留言。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<title><?php
if(is_category()){
single_cat_title();
}elseif(is_single() || is_page()){
single_post_title();
}elseif(is_search()){
echo wp_specialchars($s);
}else{
wp_title('',true);
}
if(! is_home()){
echo ' - ';
}
echo '天涯PHP博客|PHP博客';
?></title>
<?php
// http://blog.phpha.com
if(is_page()){
$description = mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 200, '...');
$keywords = $post->post_title;
}elseif(is_category()){
$description = trim(strip_tags(category_description($cat_ID)));
$keywords = single_cat_title('', false);
}elseif(is_tag()){
$keywords = single_tag_title('', false);
$description = trim(strip_tags(tag_description($tag_ID)));
}elseif(is_single()){
if($post->post_excerpt){
$description = $post->post_excerpt;
}else{
$description = mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 200, '...');
}
$keywords = $post->post_title;
}else{
$keywords = 'PHP博客,PHP,MySQL,Linux,C,编程算法,网站架构,互联网';
$description = '专注WEB开发,热爱PHP/MySQL/Linux/C/编程算法/网站架构,崇尚自由关注互联网';
}
?>
<meta name="keywords" content="<?php echo $keywords; ?>" />
<meta name="description" content="<?php echo str_replace(PHP_EOL, '', $description); ?>" />
|