0

Right now I have a front end form that lists 4 separate taxonomies in checkbox inputs that a user selects. I can list all of the terms one after the other but I can't figure out out to list them with proper hierarchy like so:

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

Right now my code to output to the form is

$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;
}
Alex
  • 21,273
  • 10
  • 61
  • 73
bilcker
  • 1,120
  • 1
  • 15
  • 43

1 Answers1

0

I have figured out a solution to my problem. It may not be the most elegant solution but I am able to achieve the layout I was looking for. Instead of get=all I replace that with the parent categories id so it only lists the children of that specific id. Since they are in a checkbox array I then perform several loops under each parent to separate the checkbox's each time just adding a new number $closed_schools_1, $closed_schools_2 and so on. would love to here from anyone who may have an easier less repetitive solution but for time being this will work.

<?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;
        }
    ?>
bilcker
  • 1,120
  • 1
  • 15
  • 43
  • Does a $closed_school object have a parent property? If so, wouldn't it be easier to create an array with all the parents, which then could have an array of children? That last one would be fairly easy to parse. – Douwe de Haan May 13 '15 at 09:44