我有一个代码,用于打印父母或子女的所有分类
<form class="navbar-form navbar-right m-t m-l-n-xs" role="search" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div class="form-group dropdown" id="ajax-search">
<input type="hidden" name="">
<div class="input-group">
<select name="download_category">
<option value="" style="display:none">My Cats</option>
<?php
$terms = get_terms([
'taxonomy' => 'download_category',
'hide_empty' => false,
'parent' => false,
]);
echo $_GET['download_category'];
foreach($terms as $ter){
?>
<option value="<?php echo $ter->slug;?>"<?php
if($_GET['download_category'] == $ter->slug){
echo "selected=\"selected\"";
}
?>><?php echo $ter->name;?></option>
<?php
}
?>
</select>
</div>
<div class="input-group">
<input type="submit" value="Search">
</div>
</div>
</form>
我需要对父母和孩子的类别进行排序 Sort category by parent with child
答案 0 :(得分:0)
您可以按名称以get_terms顺序传递参数order_by
。
$terms = get_terms([ 'taxonomy' => 'download_category',
'hide_empty' => false,
'orderby' => 'name',
'parent' => false,
]);
答案 1 :(得分:0)
<form class="navbar-form navbar-right m-t m-l-n-xs" role="search" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div class="form-group dropdown" id="ajax-search">
<input type="hidden" name="">
<div class="input-group">
<select name="download_category">
<?php $hmcatTerms = get_terms('download_category', array('hide_empty' => 0, 'parent' =>0));
foreach($hmcatTerms as $hmcatTerm) :
?>
<option value="<?php echo get_term_link( $hmcatTerm->slug, $hmcatTerm->taxonomy ); ?>">
<?php echo $hmcatTerm->name; ?>
<?php
$hmsubargs = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $hmcatTerm->term_id,
'taxonomy' => 'download_category'
);
$hmsubcats = get_categories($hmsubargs);
foreach ($hmsubcats as $hsc):
?>
<option value="<?php echo get_term_link( $hsc->slug, $hsc->taxonomy );?>">-- <?php echo $hsc->name;?></option>
<?php
endforeach;
?>
</option>
<?php
endforeach;
?>
</select>
</div>
<div class="input-group">
<input type="submit" value="Search">
</div>
</div>
</form>
我修复了我的代码问题。