今天网友问ytkah:wordpress不同分类如何调用不同的模板。我们知道in_category() 函数可以通过分类别名或ID判断当前文章所属的分类,而且可以直接在循环(Loop)内部和外部使用。首先创建一个single.php文件,复制下面的代码



<?php 
if ( in_category(array( 2,3 )) ) {//多个栏目id
get_template_part('single-product' );
} elseif ( in_category( 7 )) {//单个栏目id
get_template_part('single-case' );
} else {//其他调用默认模板
get_template_part('single-default' );
}
?>


  然后分别创建三个single-product.php,single-case.php,single-default.php,根据需要加入不同的代码

  当然也支持别名slug调用



in_category('themes')//单个别名
in_category( array( 'themes','plugins','develop') )//多个别名


  同样的道理,我们可以根据不同的分类制作不同的分类页模板,把sing改为category就可以



<?php 
if ( in_category(array( 2,3 )) ) {//多个栏目id
get_template_part('category-product' );
} elseif ( in_category( 7 )) {//单个栏目id
get_template_part('category-case' );
} else {//其他调用默认模板
get_template_part('category-default' );
}
?>


  注意is_category和in_category的区别

Try in_category() which checks if the current post is in a particular category.

The is_category() function checks if a category archive is being displayed.

  有这方面需求的朋友可以试试