wordpress获取当前分类的顶级分类ID并调用子分类

  • 函数定义

    • functions.php中定义一个函数来获取当前分类的顶级分类ID。
  • 代码示例

    // 获取分类ID,函数参数是int类型为当前分类的ID
    function tx_wp_get_category_root_id($cat) {
      $this_category = get_category($cat); // 获取当前分类的对象
      // 循环往上获得父级分类ID
      while ($this_category->category_parent) {
        $this_category = get_category($this_category->category_parent);
      }
      return $this_category->term_id;
    }
  • 分类模板中的代码

    foreach (get_categories('child_of=' . tx_wp_get_category_root_id($cat)) as $cate) {
      echo '<a href="' . get_category_link($cate->term_id) . '">' . get_cat_name($cate->term_id) . '</a>';
    }

     

posted @   黄文Rex  阅读(66)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示