以下代码自动判断页面类型,并输出相应的内容:
//根据不同的页面类型,输出相应的页面标题 <title> <?php if ( is_home() ) { bloginfo('name'); echo " - "; bloginfo('description'); } elseif ( is_category() ) { single_cat_title(); echo " - "; bloginfo('name'); } elseif (is_single() || is_page() ) { single_post_title(); } elseif (is_search() ) { echo "搜索结果"; echo " - "; bloginfo('name'); } elseif (is_404() ) { echo '页面未找到!'; } else { wp_title('',true); } ?> </title> //根据不同的页面类型,输出不同的页面关键词和描述, <?php //页面关键词和描述的判断及书写 //如果是首页 if (is_home()){ $keywords = "你网站首页的关键字,自己修改吧"; $description = "你网站首页的描述,自己修改吧"; } //如果是文章页 elseif (is_single()){ //默认使用文章页添加关键字 $keywords = get_post_meta($post->ID, "keywords", true); //如果为空,使用标签作为关键字 if($keywords == ""){ $tags = wp_get_post_tags($post->ID); foreach ($tags as $tag){ $keywords = $keywords.$tag->name.","; } //去掉最后一个逗号 $keywords = rtrim($keywords, ', '); } //默认使用文章页添加描述 $description = get_post_meta($post->ID, "description", true); //如果为空,使用文章前100个字作为描述 if($description == ""){ 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)); ?> //输出整站CSS <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />