通过父子对自定义分类中的术语进行排序

问题描述 投票:0回答:2

我有一个代码,用于打印父母或孩子的所有分类法

<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

wordpress taxonomy-terms
2个回答
0
投票

您可以通过名称传递order_by中的参数get_terms

$terms = get_terms([   'taxonomy' => 'download_category',
                       'hide_empty' => false,
                       'orderby' => 'name',
                       'parent' => false,
                   ]);

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>

我修复了我的代码问题。

© www.soinside.com 2019 - 2024. All rights reserved.