想把WordPress打造成CMS系统,需要实现 分类下下所有子分类使用同一个模板,所有该分类下的文章页也使用统一的模板。全在可以无限的使用,这样可以减少很多的代码,下面来看看我们是如何实现的。
<?php if ( in_category( '分类ID或者别名' ) || post_is_in_descendant_category( '分类ID' ) ) { // 调用的分类模版 } ?>
需要注意的是,如果子分类中没有文章,模板不生效。还有post_is_in_descendant_category( 1 )是ID为1以下的分类,不包括该分类本身。
最后在functions.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;
}
当然我们如果设置高级函数还可以在后台进行设置。这个就只能你们自己去开发了。
注意:post_is_in_descendant_category( ‘参数 ‘ )这个参数貌似只能用分类ID不能用分类别名。
文章来源:http://www.haoziw.com/zfenlei.html
评论前必须登录!
注册