使用get_categories
首先获取表单中要使用的所有帖子类别,然后遍历它们以显示多检查输入。。。
function hierarchical_category_inputs ($cat, $indent) {
$cats = get_categories(\'hide_empty=false&orderby=name&order=ASC&parent=\'.$cat);
$indent++;
if ($cats) {
foreach ($cats as $cat) {
echo "<input type=\'checkbox\' name=\'cat-".$cat->term_id."\'";
echo " style=\'margin-left: ".($indent*10)."px;\'> ".$cat->name."<br>";
hierarchical_category_inputs($cat->term_id, $indent);
}
}
}
echo "<form method=\'post\'>";
// 0 for all categories, -1 so first indent is 0
hierarchical_category_inputs(0,-1);
echo "<input type=\'hidden\' name=\'custom_catsearch\' value=\'yes\'>";
echo "<input type=\'submit\' value=\'Search\'>";
echo "</form>";
然后,在搜索部分,您将组装要传递到的已勾选类别的数组
WP_Query
:
function custom_catsearch_output() {
$cats = array();
foreach ($_POST as $key => $value) {
// make sure the post key starts with cat-
if (substr($key,0,4) == \'cat-\') {
// check for check of the checkbox
if ($value == \'1\') {
// get the cat id from after cat-
$cats[] = substr($key,4,strlen($key));
}
}
}
$query = new WP_Query(array(\'cat\' => $cats));
print_r($query);
// ... do something different with the results ...
}