WordPress 判断当前分类是否有子分类

WordPress 判断当前分类是否有子分类

WordPress 判断当前分类是否有子分类,是使用 get_term_children( int $term_id, string $taxonomy) 函数进行判断。category(分类)是一种 taxonomy,然后调用该函数,参数为该 category(分类)的 term_id 和 taxonomy,如果该分类含有子分类,那么该函数返回一个 term_id 数组,该数组中的元素是该分类的所有子分类的 term_id。而如果该分类没有子分类,则返回一个空的数组。

代码示例

<?php
global $cat; //获取当前分类
$cat_term_id = get_category($cat)->term_id; // 得到该分类的 term_id
$cat_taxonomy = get_category($cat)->taxonomy; // 得到当前分类的 taxonomy
if(sizeof(get_term_children($cat_term_id,$cat_taxonomy)) != 0) // 判断该函数返回的数组的长度
{
// 有子分类
}
else
{
// 没有子分类
}
?>

实际应用

通过判断当前分类是否有子分类,可以实现一级分类和二级子分类分别调用不同的模板。

<?php
global $cat; //获取当前分类
$cat_term_id = get_category($cat)->term_id; // 得到该分类的 term_id
$cat_taxonomy = get_category($cat)->taxonomy; // 得到当前分类的 taxonomy
if(sizeof(get_term_children($cat_term_id,$cat_taxonomy)) != 0) // 判断该函数返回的数组的长度
{
include(TEMPLATEPATH . '/category-ffl.php');
}
else
{
include(TEMPLATEPATH . '/category-zfl.php');
}
?>
温馨提示:本文最后更新于2022-05-12 20:30:30,某些文章具有时效性,若有错误或已失效,请在下方留言或联系乡野博文
您阅读这篇文章共花了: 0小时00分00秒
-----本页内容已结束,喜欢请分享!-----
© 版权声明
THE END
喜欢本站内容,请点【点赞】【分享】和【收藏】~
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容