|
不用插件给WordPress网站添加首页、文章页、页面、分类页、标签页的关键字和描述,最懒的设置办法
用编辑器打开header.php,在head与/head之间任意地方(一般加到title>下面比较美观)加上如下代码:-
- <?php
- if (is_home()){
- $keywords = "你网站首页的关键字,自己修改吧";
- $description = "你网站首页的描述,自己修改吧";
- }
- elseif (is_single()){
- $tags = wp_get_post_tags($post->ID);
- foreach ($tags as $tag){
- $keywords = $keywords.$tag->name.",";
- }
- $keywords = rtrim($keywords, ', ');
- if($post->post_excerpt){
- $description = $post->post_excerpt;
- }else{
- $description = mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,200);
- }
- }
- elseif (is_page()){
- $keywords = get_post_meta($post->ID, "keywords", true);
- $description = get_post_meta($post->ID, "description", true);
- }
- elseif (is_category()){
- $keywords = single_cat_title('', false);
- $description = category_description();
- }
- elseif (is_tag()){
- $keywords = single_tag_title('', false);
- $description = tag_description();
- }
- $keywords = trim(strip_tags($keywords));
- $description = trim(strip_tags($description));
- ?>
- <meta name="keywords" content="<?php echo $keywords; ?>" />
- <meta name="description" content="<?php echo $description; ?>" />
复制代码 OK,5个页面的关键字和描述,只要设置一次,以后全部都是自动的。如果依然觉得不好用的话,就使用插件吧!
|
|