如何按层次结构顺序显示自定义分类

时间:2014-05-27 18:06:19

标签: wordpress hierarchy frontend custom-taxonomy

现在我有一个前端表单,在用户选择的复选框输入中列出了4个单独的分类法。我可以一个接一个地列出所有术语,但我不知道如何以适当的层次结构列出它们:

-parent
    -child
    -child
    -child
-parent
    -child
    -child
 etc.

现在我输出到表单的代码是

$closed_schools = get_terms('closed_schools', 'orderby=id&order=ASC&hide_empty=0&get=all');
$counter = 0;
foreach ($closed_schools as $close) {
    $counter++;
    $option = '<fieldset id="'.$close->slug.'"><label for="'.$close->slug.'">'.$close->name.'</label>';
    $option .= '<input type="checkbox" name="terms[]" id="'.$close->slug.'" value="'.$close->slug.'">';
    $option .= '</fieldset>';
    echo $option;
}

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。它可能不是最优雅的解决方案,但我能够实现我正在寻找的布局。而不是get = all我用父类别id替换它,所以它只列出该特定id的子节点。由于它们位于复选框数组中,因此我会在每个父项下执行几个循环,以便每次添加新数字$ closed_schools_1,$ closed_schools_2等时将复选框分开。我希望任何人可以拥有一个更容易重复性更低的解决方案,但是暂时这会有效。

<?php
    $closed_schools = get_terms('closed_schools', 'orderby=id&order=ASC&hide_empty=0&child_of=35');
    $counter = 0;
    foreach ($closed_schools as $closed_school) {
        $counter++;
        $option = '<fieldset id="'.$closed_school->slug.'">
            <label for="'.$closed_school->slug.'">'.$closed_school->name.'</label>';
            $option .= '<input type="checkbox" name="closed_school_terms[]" id="'.$closed_school->slug.'" value="'.$closed_school->slug.'">';
            $option .= '</fieldset>';
            echo $option;
    }
?>
<?php
        $closed_schools_2 = get_terms('closed_schools', 'orderby=id&order=ASC&hide_empty=0&child_of=55');
        $counter = 0;
        foreach ($closed_schools_2 as $closed_school_2) {
            $counter++;
            $option_2 = '<fieldset id="'.$closed_school_2->slug.'">
                <label for="'.$closed_school_2->slug.'">'.$closed_school_@->name.'</label>';
                $option_2 .= '<input type="checkbox" name="closed_school_terms[]" id="'.$closed_school_2->slug.'" value="'.$closed_school_2->slug.'">';
                $option_2 .= '</fieldset>';
                echo $option_2;
        }
    ?>