每篇文章的内容不同,我们该如何让wordpress自动添加文章描述和关键词呢?办法很简单,我们只需要在模板头部添加如下一段PHP代码。
打开你的模板文件header.php,在最前面添加如下PHP代码:
< ? php
//判断是否为首页
i f ( is_home ( ) ) {
$ description = "博客首页的描述,最多220字符"
$ keywords = "博客首页的关键词,用英文逗号分开"
//判断是否为文章页
} else i f ( is_single () ) {
i f ( $ post->post_excerpt) {
$ description = $ post – > post_excerpt;
} else {
$ description = mb_strimwidth(strip_tags(
apply_filters(‘the_content’,$post->post_content)
),0,220);
}
$ keywords = “”;
$ tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag ) {
$ keywords = $ keywords . $ tag->name . “,”;
}
//判断是否为分类页
} else if ( is_category() ) {
$ description = category_description();
}
? >
查找标签: < / head >
在前面加入 < meta name = " keywords " content = " < ? php echo $keywords; ? >” / >
< meta name= " description" content = " < ?php echo $description; ? > ” / >
大功告成!
这样是不是就用不到 all in one seo pack 这个优化插件了?会不会被BD或者GG误判为恶意堆砌关键字啊什么的? 球解,TKS…
关键字就是你自己设置的tag,一个关键字只会出现一次
对于搜索引擎优化来说,关键字还是自己设置的好。
tag按理应该就是关键字
又是关键字问题,但是我感觉用插件会方便点。