请问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>'; }
更新时间:2025-04-10 23:31:45
上一篇:请问如何给自己的公司建网站
下一篇:请问如何把网页完整保存下载,修改网页内容,刷新数据保持不变方法
转载请注明原文链接:https://www.muzicopy.com/suibi/2398.html