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

作者:

在

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

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

<?php   
      $post = $wp_query->post;   
      if(in_category('16')) {   
      include(TEMPLATEPATH.'/category_16.php');   
      }   
      else if (in_category('7')){   
      include(TEMPLATEPATH.'/category_7.php');   
      }   
      else {   
      include(TEMPLATEPATH.'/category-default.php');   
      }   
 ?>  

最终结果如我这样既可。

<?php get_header(); ?>   
 <?php   
         $post = $wp_query->post;   
         if(in_category('16')) {   
         include(TEMPLATEPATH.'/category_16.php');   
         }   
         else if (in_category('7')){   
         include(TEMPLATEPATH.'/category_7.php');   
         }   
         else {   
         include(TEMPLATEPATH.'/category-default.php');   
         }   
 ?>   
 <?php get_footer(); ?>  

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

<?php get_header(); ?>   
 <?php   
         $post = $wp_query->post;   
         if(in_category('16')) {   
         include(TEMPLATEPATH.'/category_16.php');   
         }   
         else if (in_category('7')){   
         include(TEMPLATEPATH.'/category_7.php');   
         }   
         else if (in_category('8')){   
         include(TEMPLATEPATH.'/category_8.php');   
         }   
         else {   
         include(TEMPLATEPATH.'/category-default.php');   
         }   
 ?>   
 <?php get_footer(); ?>  

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

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

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

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

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

function post_is_in_descendant_category( $cats, $_post = null )   
 {   
 foreach ( (array) $cats as $cat ) {   
 // get_term_children() accepts integer ID only   
 $descendants = get_term_children( (int) $cat, 'category');   
 if ( $descendants && in_category( $descendants, $_post ) )   
 return true;   
 }   
 return false;   
 }  

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

<?php if ( in_category('16') || post_is_in_descendant_category(16) )   
 {include(TEMPLATEPATH .'/single-16.php');}   
    
elseif( in_category('7') || post_is_in_descendant_category(7) )   
 {include(TEMPLATEPATH . '/single-7.php');}   
    
else{include(TEMPLATEPATH . '/single-default.php');}   
 ?>  

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

<?php get_header(); ?>   
 <?php if ( in_category('16') || post_is_in_descendant_category(16) )   
 {include(TEMPLATEPATH .'/single-16.php');}   
 elseif( in_category('7') || post_is_in_descendant_category(7) )   
 {include(TEMPLATEPATH . '/single-7.php');}   
 else{include(TEMPLATEPATH . '/single-default.php');}   
 ?>   
 <?php get_footer(); ?>  

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

评论

发表回复

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