首先获得数组织
$tree = taxonomy_get_tree(1); //taxonomy_get_tree 中的参数,是分类 1 级的id。
其次,把获取的数组分级,并且生成表单控件。
foreach($tree as $value){
if (!empty($value)) {
if(in_array($value->tid,$tid_str)){
$checked = 1;
}else{
$checked = 0;
}
//子类 则 用‘-’ 来分开,具体用多少‘-’是根据 下面 $value['depth'] 深度这个参数来判断的
$chk_value = str_repeat('-', $value->depth) . $value->name;
//生成表单控件
$form['pid'.$value->tid] = array(
'#type' => 'checkbox',
'#title' => $chk_value,
'#default_value' => $checked,
'#attributes' => array('class' => array('menu-title-select')),
);
}
}
生成的效果图为:
![](https://i-blog.csdnimg.cn/blog_migrate/de1b09fdae2e29e8889b208a43547186.jpeg)