|  | 
 
| 使用 in_category() 进行判断 in_category() 函数可以通过分类别名或ID判断当前文章所属的分类,而且可以直接在循环(Loop)内部和外部使用。
 
 如果是单个分类 ID ,比如ID 为 2 ,可以这样写
 
 in_category(2)
 如果是单个分类别名,比如别名为 themes,可以这样写
 
 in_category('themes')
 如果是多个ID,可以这样写
 
 in_category( array( 2,3,7) )
 如果是多个别名,可以这样写
 
 in_category( array( 'themes','plugins','develop') )
 然后我们结合 if 语句就可以很好地实现模板的选择。比如我们可以在主题的根目录创建3个文章模板文件,分别命名为 single001.php , single002.php 和 single003.php,然后我们希望 ID 为 2 和 3 的分类使用 single001.php,ID为 7 的分类使用 single002.php ,其他分类使用 single003.php,那么,我们可以在 single.php 文件写入下面的代码:
 
 
 复制代码<?php
if ( in_category(array( 2,3 )) ) {
    get_template_part('single001' );
} elseif ( in_category( 7 )) {
    get_template_part('single002' );
} else {
    get_template_part('single003' );
}
?>
 | 
 |