term_id] = true; } } foreach ($terms as $term) { if (!$term instanceof WP_Term) { continue; } $pid = (int) $term->parent; if ($pid !== 0 && !isset($present[$pid])) { $pid = 0; } if (!isset($children_map[$pid])) { $children_map[$pid] = []; } $children_map[$pid][] = $term; } $render = static function (int $pid, int $d) use (&$render, $children_map, $checked, $name): void { if (empty($children_map[$pid])) { return; } foreach ($children_map[$pid] as $term) { $tid = (int) $term->term_id; $pad = $d > 0 ? ' style="margin-left:' . (int) ($d * 16) . 'px"' : ''; echo ''; $render($tid, $d + 1); } }; $render($parent, $depth); } endif;