天兴工作室

主题演示站

wordpress调用当前分类的全部子分类

wordpress在制作企业主题的时候比较常遇到要调用当前分类下的全部子分类。要实现这个可以按照以下步骤操作。

首先在主题的“functions.php”里面写个函数,代码如下:

// 获取子分类
function get_category_root_id($cat){
    $this_category = get_category($cat);
    while($this_category->category_parent) {
        $this_category = get_category($this_category->category_parent);
    }
    return $this_category->term_id;
}

然后在需要调用的页面里写以下代码:

<?php
if(is_category()) {
    $cat = get_query_var('cat');
    $categoryurl = get_category_link($cat);
    if(get_category_children(get_category_root_id(the_category_ID(false)))!= "" ) {
        echo wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=&orderby=id&order=ASC");
    }
}
?>

最后自己调整下css就ok了。

«    2025年5月    »
1234
567891011
12131415161718
19202122232425
262728293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接

Powered By Z-BlogPHP 1.7.4

天兴工作室主题演示站点,如遇到问题请联系我们的在线qq:207649916咨询