生命不息,奋斗不止/创造价值-传递价值-获得价值
所谓迷茫,就是才华配不上梦想 每一个让你难堪的现在,都有一个不够努力的曾经

WordPress子分类继承父分类模板样式

想把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

赞(0)
未经允许不得转载:jack361博客 » WordPress子分类继承父分类模板样式

如果你爱我或恨我,请↓

联系我