我需要对类别列表输出进行更多控制,因此我使用get_categories()
, 而不是wp_list_categories()
.
此函数返回按特定属性排序的对象的平面数组。如何从中构建层次结构列表,如wp_list_categories()
做
我需要对类别列表输出进行更多控制,因此我使用get_categories()
, 而不是wp_list_categories()
.
此函数返回按特定属性排序的对象的平面数组。如何从中构建层次结构列表,如wp_list_categories()
做
遍历数据的最理想解决方案是使用WordPress walker类。
http://codex.wordpress.org/Function_Reference/Walker_Class
你在网上找不到很多使用它的例子,但Scribu在这里给出了一个例子。http://scribu.net/wordpress/extending-the-category-walker.html
您还可以查看WordPress用于扩展walker的类作为进一步的示例。http://core.trac.wordpress.org/browser/tags/3.0.1/wp-includes/classes.php
希望这有帮助。。get_categories
$categories = get_categories();
// First index all categories by parent id, for easy lookup later
$cats_by_parent = array();
foreach ($categories as $cat)
{
$parent_id = $cat->category_parent;
if (!array_key_exists($parent_id, $cats_by_parent))
{
$cats_by_parent[$parent_id] = array();
}
$cats_by_parent[$parent_id][] = $cat;
}
// Then build a hierarchical tree
$cat_tree = array();
function add_cats_to_bag(&$child_bag, &$children)
{
global $cats_by_parent;
foreach ($children as $child_cat)
{
$child_id = $child_cat->cat_ID;
if (array_key_exists($child_id, $cats_by_parent))
{
$child_cat->children = array();
add_cats_to_bag($child_cat->children, $cats_by_parent[$child_id]);
}
$child_bag[$child_id] = $child_cat;
}
}
add_cats_to_bag($cat_tree, $cats_by_parent[0]);
我有一个有趣的问题,希望有人能尽快回答。我已经创建了自己的metabox,它基于“我的metabox代码”(下面的列表)正确地显示了我创建的“event\\u types”分类中所有术语的下拉列表。我遇到的问题是,当从下拉列表中选择不同的术语并更新帖子时,能够保存/更新与帖子相关的术语。在对各种代码位进行修补之后,我发现通过手动将term\\u ID number[用逗号分隔]输入数组区域,我得到了我想要的结果。例如,如果在保存帖子时,函数将调用此代码wp_set_post_terms( $post_id