在wordpress中只显示一级子类别

Ful*_*top 2 wordpress

这是我的问题示例:

监视器(主类别)

  • 三星(第一级子类别)

    • lcd(二级子类别)

这是我的代码:

$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>';
}
Run Code Online (Sandbox Code Playgroud)

Nik*_*shi 6

我已经添加了这个片段,其中一个在我这边已经过全面测试,并且也在我的项目中使用过。

$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 } ?>
Run Code Online (Sandbox Code Playgroud)

您可以将其自定义为您的项目要求,我只是给您提供基于父 ID 获取子类别的想法。

谢谢希望这对你有用。