获取特定类别的子类别并在li标签中显示子类别

时间:2018-12-09 16:31:14

标签: wordpress woocommerce categories

我使用此代码显示woocommerce商店的主要类别现在,我想使用类别名称显示该类别的子类别

我的代码

<?php
function woocommerce_subcats_from_parentcat_by_NAME($parent_cat_NAME) {
    $IDbyNAME = get_term_by('name', $parent_cat_NAME, 'product_cat');
    $product_cat_ID = $IDbyNAME->term_id;
    $args = array(
        'hierarchical' => 1,
        'show_option_none' => '',
        'hide_empty' => 0,
        'parent' => $product_cat_ID,
        'taxonomy' => 'product_cat'
    );
    $subcats = get_categories($args);
    foreach ($subcats as $sc) {
        $link = get_term_link( $sc->slug, $sc->taxonomy );
        echo '<div class="col-md-4" title="'.$sc->name.'">
              <a  href="'. $link .'" ><div class="section-header">  '.$sc->name.'</div> </a> 
               <ul class="section-header-ul">
               <li><i class="fas fa-caret-left"></i><a href="">لبتاب</a> </li>
               </ul>
              </div>';
    }
}
?>
<?php woocommerce_subcats_from_parentcat_by_NAME("electronic_components"); ?>

1 个答案:

答案 0 :(得分:0)

<?php 

$taxonomyName = "com_category";
$parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => false));   

foreach ($parent_terms as $pterm) {
    $terms = get_terms($taxonomyName, array('parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false));
    foreach ($terms as $term) {
        echo '<div class="single_cat col-md-3">';
        echo '<h3>'.$pterm->name.'</h3>'; 
        echo "<ul>";
        echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>'; 
        echo "</ul>";
        echo '</div>'; 
    }
}


 ?>