Pregunta

I have the below code which is pulling in all child categories including grand children - how can I edit this so it ONLY pulls in the direct children?

 $subCategories = get_term_children( $categoryID, 'product_club' );

$subcategoryData = '[';
if(!empty($subCategories)) {
$isFirst = true;
foreach($subCategories as $subCategory) {
  $term = get_term_by( 'id', $subCategory, 'product_club' );
  if(!$isFirst) $subcategoryData .= ', ';
  $isFirst = false;
  $subcategoryData .= '{"id": '.$term->term_id.', "name": "'.$term->name.'"}';
}
}
$subcategoryData .= ']';
echo $subcategoryData;
die;

Big thanks!!

¿Fue útil?

Solución

May need some adjusting. This will get all direct children of $categoryID of taxonomy type 'product_club'.

$args = array(
    'child_of' => $categoryID,
    'taxonomy' => 'product_club',
    'hide_empty' => 0,
    'hierarchical' => true,
    'depth'  => 1,
    );
$cats = get_categories( $args );

The magic parameter is 'depth'.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top