如何在前端展示层级taxonomy的多选复选框并修改现有代码
原生<select>标签本身不支持嵌入复选框,所以需要替换为自定义的折叠多选结构,完整修改方案如下:
1. 替换原有PHP代码
把你原来的select相关代码全部替换为以下内容:
<?php /** The taxonomy we want to parse */ $taxonomy = "property_type_taxonomy"; /** Get all taxonomy terms */ $terms = get_terms($taxonomy, array( "orderby" => "count", "hide_empty" => false ) ); /** Get terms that have children */ $hierarchy = _get_term_hierarchy($taxonomy); ?> <!-- 自定义下拉容器 --> <div class="taxonomy-multiselect"> <div class="multiselect-header">请选择房产类型</div> <div class="multiselect-options"> <?php /** Loop through every term */ foreach($terms as $term) { /** Skip term if it has children */ if($term->parent) { continue; } // 输出父分类选项 echo '<label class="option-item parent-option"> <input type="checkbox" name="terms[]" value="'.esc_attr($term->name).'"> '.esc_html($term->name).' </label>'; /** If the term has children... */ if($hierarchy[$term->term_id]) { /** ...display them */ foreach($hierarchy[$term->term_id] as $child) { /** Get the term object by its ID */ $child = get_term($child, "property_type_taxonomy"); // 输出子分类,加缩进 echo '<label class="option-item child-option"> <input type="checkbox" name="terms[]" value="'.esc_attr($child->name).'"> '.esc_html($child->name).' </label>'; } } } ?> </div> </div>
注意这里name="terms[]"用了数组格式,后端提交时可以直接接收所有选中的分类值。
2. 添加CSS样式
将以下CSS加入到你的主题样式文件中,实现下拉折叠、层级缩进效果:
.taxonomy-multiselect { position: relative; width: 300px; font-size: 14px; } .multiselect-header { padding: 8px 12px; border: 1px solid #ddd; border-radius: 4px; background: #fff; cursor: pointer; } .multiselect-options { display: none; position: absolute; top: 100%; left: 0; right: 0; border: 1px solid #ddd; border-top: none; background: #fff; max-height: 300px; overflow-y: auto; z-index: 99; } .taxonomy-multiselect.open .multiselect-options { display: block; } .option-item { display: block; padding: 6px 12px; cursor: pointer; } .option-item:hover { background: #f5f5f5; } .child-option { padding-left: 28px; } .option-item input { margin-right: 8px; }
3. 添加JavaScript交互
将以下JS加入到你的主题脚本文件中,实现下拉展开/收起、父子级联动选中功能:
document.addEventListener('DOMContentLoaded', function() { // 下拉展开收起 const selectHeader = document.querySelector('.multiselect-header'); const selectContainer = document.querySelector('.taxonomy-multiselect'); selectHeader.addEventListener('click', function() { selectContainer.classList.toggle('open'); }); // 点击页面其他区域关闭下拉 document.addEventListener('click', function(e) { if (!selectContainer.contains(e.target)) { selectContainer.classList.remove('open'); } }); // 父级选中自动选中所有子级(可选功能,不需要可以删掉这部分) const parentCheckboxes = document.querySelectorAll('.parent-option input'); parentCheckboxes.forEach(parentCheckbox => { parentCheckbox.addEventListener('change', function() { const childOptions = []; let nextEl = this.closest('.parent-option').nextElementSibling; while(nextEl && nextEl.classList.contains('child-option')) { childOptions.push(nextEl.querySelector('input')); nextEl = nextEl.nextElementSibling; } childOptions.forEach(child => { child.checked = this.checked; }); }); }); });
如果你不需要父级选中联动子级的功能,删掉最后一段父级联动的代码即可。
内容的提问来源于stack exchange,提问作者Sam
相关产品推荐
相关产品推荐

