本段代码加入了独立Page页与分类页的 Description 和 Keywords,功能可与 All in One SEO 插件相媲美。注:分类面与独立Page页若未使用默认链接,则需将代码中的数字ID改为自己设定的别名。
修改方法:
把 header.php 中的(不一定完全匹配,认准
1 | <title><?php wp_title('«', true, 'right'); ?> <?php bloginfo('name'); ?></title> |
改为:
1 | <?php include_once("seo.php"); ?> |
把以下代码保存为 seo.php 放置在主题目录中。
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | <!--###Title Begin, By Lc.###--> <?php if ( is_home() ) { ?><title><?php bloginfo('name'); ?> | <?php bloginfo('description'); ?></title><?php } ?> <?php if ( is_search() ) { ?><title>搜索结果 | <?php bloginfo('name'); ?></title><?php } ?> <?php if ( is_single() ) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo('name'); ?></title><?php } ?> <?php if ( is_page() ) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo('name'); ?></title><?php } ?> <?php if ( is_category() ) { ?><title><?php single_cat_title(); ?> | <?php bloginfo('name'); ?></title><?php } ?> <?php if ( is_month() ) { ?><title><?php the_time('F'); ?> | <?php bloginfo('name'); ?></title><?php } ?> <?php if (function_exists('is_tag')) { if ( is_tag() ) { ?><title><?php single_tag_title("", true); ?> | <?php bloginfo('name'); ?></title><?php } ?> <?php } ?> <?php ##定义一个函数.解决截取中文乱码的问题### if (!function_exists('utf8Substr')) { function utf8Substr($str, $from, $len) { return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'. '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s', '$1',$str); } } if ( is_home() ){ $description = "我们身处急速上涨的数据海洋中…我们如何避免信息的没顶之灾呢?柳城博客(Lc.)∷关注生物信息学,分享学习Linux、PHP+Mysql、Perl/BioPerl等的心得,努力在数据的海洋里畅游。"; $keywords = "生物信息学,Perl,Bioperl,PHP,Mysql,Linux,NCBI,摄影"; } elseif ( is_single() ){ if ($post->post_excerpt) { $description = $post->post_excerpt; } else { if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){ $post_content = $result['1']; } else { $post_content_r = explode("\n",trim(strip_tags($post->post_content))); $post_content = $post_content_r['0']; } $description = utf8Substr($post_content,0,220); } $keywords = ""; $tags = wp_get_post_tags($post->ID); foreach ($tags as $tag ) { $keywords = $keywords . $tag->name . ","; } } ###这里是分类页面。自行改变is_category的ID。### elseif ( is_category(34) ){ $description = "生物信息学(Bioinformatics)是一门利用计算机技术研究生物系统之规律的学科。通过实例分析,介绍生物信息学的入学知识,包含生物信息学的数据库等。重点是NCBI的中文教程。"; $keywords = "生物信息学,Bioinformatics,NCBI,影响因子"; } ###这里是Page页。同上。多个页面的话自行添加就是### elseif ( is_page(2) ){ $description = "关于柳城博客(Lc.)的介绍,联系方式,以及网站历程。柳城博客(LIUCHENG.NAME)∷努力在数据的海洋里畅游。"; $keywords = "生物信息学,Perl,Bioperl,PHP,Mysql,Linux,NCBI,摄影"; } elseif ( is_page(135) ){ $description = "柳城博客(Lc.)的留言板。有什么问题或建议请在这里留言! 我会尽快回复~ 感谢您的支持!!"; $keywords = "柳城博客,Lc.,留言板,留言本"; } ?> <?php echo "\n"; ?> <meta name="description" content="<?php echo trim($description); ?>" /> <meta name="keywords" content="<?php echo rtrim($keywords,','); ?>" /> <!--###Description & Keywords End, By Lc.###--> |
Pingback: 温和一点 | 千里走单骑