仅显示wordpress中的第一级子类别

时间:2018-12-26 12:20:30

标签: 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>';
}

1 个答案:

答案 0 :(得分:1)

我添加了这些代码片段,这些代码片段已经从我的角度进行了全面测试,并且也在我的项目中使用过。

$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获取子类别。

谢谢,希望对您有用。

相关问题