代码之家  ›  专栏  ›  技术社区  ›  Full Stop

在wordpress中只显示一级子类别

  •  0
  • Full Stop  · 技术社区  · 7 年前

    下面是我的问题示例:

    • 三星(一级子类别)

    $term_id = $catid;
    $taxonomy_name = 'providers-category';
    $term_children = get_term_children( $term_id, $taxonomy_name );
    
    if(count($term_children) > 0){
    
        echo '<div class="container"><div class="checkboxes"><ul>';
    foreach ( $term_children as $child ) {
        $term = get_term_by( 'id', $child, $taxonomy_name );
        echo '<li> <input class="chk_child_cat" type="checkbox" value="1" data-id="'. get_term_link( $child, $taxonomy_name ).'"/><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
    }
    echo '</ul></div></div>';
    }else{
     echo '<div><h2><b>Opps!...Sub category not found.<b></h2></div>';
    }
    
    1 回复  |  直到 7 年前
        1
  •  5
  •   Niket Joshi    7 年前

    我已经添加了这个片段,其中一个是从我的立场充分测试,并在我的projet中也使用了这个。

    $term_id = $catid; /* Assume that here is your parent ID */
    $taxonomy_name = 'providers-category'; /* Want child of the parent category */
     $args = array(
                  'hide_empty' => 0,
                  'orderby' => 'name',
                  'depth' => 1,
                  'parent' => $term_id,
                  'order' => 'ASC',
                  'taxonomy' => 'providers-category'
              );
      $term_children = get_categories($args);
       foreach ( $term_children as $child ) {
      ?>
              <li class="city-item"><a href="<?php  echo get_term_link( $child, $taxonomy_name )   ?>"><?php echo $child->name;  ?></a></li>
      <?php } ?>
    

    您可以将其定制为您的项目需求,我只是给您一个基于父ID获取子类别的想法。

    谢谢,希望这对你有用。