admin管理员组

文章数量:1425815

Hi basically I have categori ID number (134) that has 2 child of that (red and blue). I selected the blue one. I want to display only the selected one (blue). But at the moment the show all subcategories (red and blue). Please help me here my code

$args = array(
   'hierarchical' => 1,
   'show_option_none' => '',
   'hide_empty' => 0,
   'parent' => 134,
   'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<ul class="wooc_sclist">';
  foreach ($subcats as $sc) {
    $link = get_term_link( $sc->slug, $sc->taxonomy );
      echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
  }
echo '</ul>';

Here for single product page.

Hi basically I have categori ID number (134) that has 2 child of that (red and blue). I selected the blue one. I want to display only the selected one (blue). But at the moment the show all subcategories (red and blue). Please help me here my code

$args = array(
   'hierarchical' => 1,
   'show_option_none' => '',
   'hide_empty' => 0,
   'parent' => 134,
   'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<ul class="wooc_sclist">';
  foreach ($subcats as $sc) {
    $link = get_term_link( $sc->slug, $sc->taxonomy );
      echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
  }
echo '</ul>';

Here for single product page.

Share Improve this question asked Jun 18, 2019 at 4:33 Wilda SagitaWilda Sagita 34 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

To display only assigned category you need to change hide_empty to true.

Please see the modified code:

$args = array(
 'hierarchical' => 1,
 'show_option_none' => '',
 'hide_empty' => true,
 'parent' => 134,
 'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<ul class="wooc_sclist">';
foreach ($subcats as $sc) {
$link = get_term_link( $sc->slug, $sc->taxonomy );
  echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
}
echo '</ul>';

Please see the updated code:

$cats_list = get_the_terms ( get_the_ID() , 'product_cat' );

echo '<ul class="wooc_sclist">';
foreach ($cats_list as $cats) {
$link = get_term_link( $cats->slug, $cats->taxonomy );
echo '<li><a href="'. $link .'">'.$cats->name.'</a></li>';
}
echo '</ul>';

本文标签: phpShow subcategory name selected in specific category woocoommerce