Chinaunix首页 | 论坛 | 博客
  • 博客访问: 337902
  • 博文数量: 71
  • 博客积分: 2129
  • 博客等级: 大尉
  • 技术积分: 835
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-18 16:03
文章分类

全部博文(71)

文章存档

2014年(22)

2013年(18)

2012年(24)

2010年(7)

我的朋友

分类: PHP

2014-01-07 11:32:49

如何为WP设置不同分类使用不同的文章列表模板
不同分类页用不同模板之文章详情页:

在Wordpress主题的文章详情页页:single.php之类,备份原有主循环,然后这么做。

  1. $post = $wp_query->post;   
  2. if (in_category('1')) {   
  3.     include(TEMPLATEPATH.'/single-01.php');   
  4. elseif (in_category('2')) {   
  5.     include(TEMPLATEPATH.'/single-02.php');   
  6. else {   
  7.     include(TEMPLATEPATH.'/single-default.php');   
  8. } ?>  

当然,也可以这样做:

  1. add_filter('single_template', create_function('$t', 'foreach((array) get_the_category() as $cat) { if (file_exists(TEMPLATEPATH . "/single-{$cat->term_id}.php")) return TEMPLATEPATH . "/single-{$cat->term_id}.php"; } return $t;'));   
  2.  ?>  

 

不同分类页用不同模板之分类页面

如果想为Wordpress众多文章分类中的某一类使用特定的文章列表模板,也很简单,例如:

  1. function getnewscatid($slug){   
  2.   
  3.     $icat=get_category_by_slug($slug);   
  4.     $icat_links=get_category_link($icat->term_id);   
  5.     $icat_name=$icat->name;   
  6.     $cat_id=$icat->term_id;   
  7.     return $cat_id;   
  8. }   
  9. //Use your NEWS category slug to replace this string below:my-news.   
  10. $newscatid=getnewscatid('my-news');   
  11. define('NEWSCATID',$newscatid);  

上面的实例是获取分类slug为my-news的分类的ID,并将之定义为常量NEWSCATID,然后自定义一个适用于这个分类的文章列表页面模 板template_news.php,在category.php或者archive.php之类的页面你会找到输出文章列表的主循环,在主循环的外层 加上:

  1. if(in_category(NEWSCATID)){include('template_news.php');}else{   
  2. //the origin output loop.   
  3. }  
阅读(2247) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~