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">
                    <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>

我需要对父级和子级中的类别进行排序,例如按父级和子 级排序类别

4

2 回答 2

0

您可以按名称order_byget_terms顺序传递参数。

$terms = get_terms([   'taxonomy' => 'download_category',
                       'hide_empty' => false,
                       'orderby' => 'name',
                       'parent' => false,
                   ]);
于 2018-03-02T04:18:29.150 回答
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>

我已经解决了我的代码问题。

于 2018-03-02T08:31:43.833 回答