WordPress不同分类与目录调用不同模板的方法

首页 » WordPress不同分类与目录调用不同模板的方法

为不同的分类添加模板文件

首先我们要做的就是找到你网站正在使用的主题文件(默认路径..\wp-content\themes\),并用编辑器打开category.php文件,然后用下面的代码替换里面除get_header()与get_footer()除外的代码,并将原来被替换的代码拷贝出来并粘贴到你新建的模板文件中,如category_default.php

Code   ViewCopyPrint
  1. <?php   
  2.       $post = $wp_query->post;   
  3.       if(in_category(’16’)) {   
  4.       include(TEMPLATEPATH.’/category_16.php’);   
  5.       }   
  6.       else if (in_category(‘7’)){   
  7.       include(TEMPLATEPATH.’/category_7.php’);   
  8.       }   
  9.       else {   
  10.       include(TEMPLATEPATH.’/category-default.php’);   
  11.       }   
  12.  ?>  

最终结果如我这样既可。

Code   ViewCopyPrint
  1. <?php get_header(); ?>   
  2.  <?php   
  3.          $post = $wp_query->post;   
  4.          if(in_category(’16’)) {   
  5.          include(TEMPLATEPATH.’/category_16.php’);   
  6.          }   
  7.          else if (in_category(‘7’)){   
  8.          include(TEMPLATEPATH.’/category_7.php’);   
  9.          }   
  10.          else {   
  11.          include(TEMPLATEPATH.’/category-default.php’);   
  12.          }   
  13.  ?>   
  14.  <?php get_footer(); ?>  

这段代码函数的主要作用就是根据分类目录的ID去判断并调用对应的模板,如果分类目录ID为16,则为这个分类目录调用category_16.php模板,如果ID为7,则调用category_7.php模板,如果以上两者都不是则调用category-default.php这个默认的模板。当然了,如果你如果需要给更多的分类目录指定模板,你只需要再添加一个else if语句既可,如下面代码所示:

Code   ViewCopyPrint
  1. <?php get_header(); ?>   
  2.  <?php   
  3.          $post = $wp_query->post;   
  4.          if(in_category(’16’)) {   
  5.          include(TEMPLATEPATH.’/category_16.php’);   
  6.          }   
  7.          else if (in_category(‘7’)){   
  8.          include(TEMPLATEPATH.’/category_7.php’);   
  9.          }   
  10.          else if (in_category(‘8’)){   
  11.          include(TEMPLATEPATH.’/category_8.php’);   
  12.          }   
  13.          else {   
  14.          include(TEMPLATEPATH.’/category-default.php’);   
  15.          }   
  16.  ?>   
  17.  <?php get_footer(); ?>  

另外要注意的就是category_8.php等这些模板文件的调用路径了,如果你想单独新建一个文件夹来放这些分类目录模板文件,那上面代码中也要一起修改。

到这里为不同的分类目录调用不同的模板就结束了,最后你要做的就是根据自己的完美思想去定义模板文件了。

为不同的文章添加模板文件

为文章添加模板与为分类目录添加模板有点不一样,Wordpress中打开一篇文章默认都是先调用single.php文件,所以我们只需要在single.php文件中添加条件判断函数即可,最常用的是in_category()函数,通过判断文章属于哪个分类,从而调用指定模板。

首先找到你模板文件中的function.php文件,将下面的代码添加到文件中。

Code   ViewCopyPrint
  1. function post_is_in_descendant_category( $cats$_post = null )   
  2.  {   
  3.  foreach ( (array$cats as $cat ) {   
  4.  // get_term_children() accepts integer ID only   
  5.  $descendants = get_term_children( (int) $cat, ‘category’);   
  6.  if ( $descendants && in_category( $descendants$_post ) )   
  7.  return true;   
  8.  }   
  9.  return false;   
  10.  }  

然后打开模板文件中的single.php文件,并用下面的代码替换里面除get_header()与get_footer()函数除外的代码,你也可以将single.php中被替换的代码拷贝到你新建的模板文件中,如single-default.php,这一点与分类目录中的代码替换一样。

Code   ViewCopyPrint
  1. <?php if ( in_category(’16’) || post_is_in_descendant_category(16) )   
  2.  {include(TEMPLATEPATH .’/single-16.php’);}   
  3.     
  4. elseif( in_category(‘7’) || post_is_in_descendant_category(7) )   
  5.  {include(TEMPLATEPATH . ‘/single-7.php’);}   
  6.     
  7. else{include(TEMPLATEPATH . ‘/single-default.php’);}   
  8.  ?>  

最后single.php文件的代码像下面所以即可。

Code   ViewCopyPrint
  1. <?php get_header(); ?>   
  2.  <?php if ( in_category(’16’) || post_is_in_descendant_category(16) )   
  3.  {include(TEMPLATEPATH .’/single-16.php’);}   
  4.  elseif( in_category(‘7’) || post_is_in_descendant_category(7) )   
  5.  {include(TEMPLATEPATH . ‘/single-7.php’);}   
  6.  else{include(TEMPLATEPATH . ‘/single-default.php’);}   
  7.  ?>   
  8.  <?php get_footer(); ?>  

上面代码表示在分类ID为16和ID16以下所有子分类的所有文章都会调用single-16.php这个模板文件,ID为7则调用single-7.php,反之则调用默认的single-default.php模板文件。如果你还需要为某个分类下的文章添加模板,你就只需要再添加一条else if语句和新建一个single模板文件即可。


发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注