SEO这东西人云亦云,但不管怎样 title、description、keywords 这三个标签是不能忽视的,为了废掉 All in one seo 这插件,我对主题进行了些修改,使之可以自定义description 和 keywords` 。
编辑主题文件中的 header.php 文件,在head标签里面添加一些代码即可。原理也很简单,也就是使用了 wordpress 的自定义字段的功能。如果文章有自定义字段“desc”则输出其值,否则输出摘要或者自动截取的文摘(默认字符数为100),作为 description,同理,如果有自定义字段“key”则输出,否则以文章的tag,作为 keywords
另外,除了 home、single 和 page 页面外,其他的全部 noindex,nofollow 了,这样世界就清净多了…
php
<?php
if (is_single()) {
//如果有自定义字段“desc”则输出,否则输出摘要或者自动截取的文摘,作为description
if ( get_post_meta($post->ID, 'desc', true) ) {
$desc = get_post_meta($post->ID, 'desc', true);
} else if ($post->post_excerpt) {
$desc = $post->post_excerpt;
} else {
$desc =mb_substr(strip_tags($post->post_content),0,100);
}
//如果有自定义字段“key”则输出,否则输出文章的tag,作为keywords
if ( get_post_meta($post->ID, 'key', true) ) {
$key = get_post_meta($post->ID, 'key', true);
} else {
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag ) {
$key = $key.$tag->name.",";
}
}
} else if (is_category()) {
$desc =category_description();
}else{
$key = "WangYan,Blog,博客,日志,网页,设计,web,design,网络,web2.0,资源,下载";
$desc = get_bloginfo('description');
}
// 自定义搜索引擎索引方法
if (is_home() || is_single() || is_page()){
$robots = "index,follow,noodp,noydir";
} else {
$robots = "noindex,nofollow,noodp,noydir";
}
?>
<meta name="keywords" content="<?php echo $key ?>" />
<meta name="description" content="<?php echo $desc ?>" />
<meta name="robots" content="<?php echo $robots ?>" />