提供zblog模板_zblog主题_wordpress模板的下载和定制

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

天兴工作室 2019-12-25 20:32 wordpress教程 3343 0 评论


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了。


没有找到能解决你问题的教程?

您可以试着搜索一下或者直接在线提问。我们也提供收费技术支持,有需要可以在线联系我们。

在线提问 在线客服

欢迎 发表评论: 取消回复

请填写验证码
  • 最新文章
  • 热文排行
  • 最多评论
标签聚合
  • 登 录
  • 注册账号 忘记密码?
  • 注 册
  • 已有账号?直接登录 忘记密码?
  • 社交账号登录