9
votes

please, can any one help me , I am new in wordpress world. how to get sub categories by parent category id in wordpress?

2
So you want to find all the terms of a taxonomy whose parent is $id?Nick M

2 Answers

15
votes

You need to use get_terms($taxonomies, $args);

$parent_term_id = 4; // term id of parent term (edited missing semi colon)

$taxonomies = array( 
    'category',
);

$args = array(
    'parent'         => $parent_term_id,
    // 'child_of'      => $parent_term_id, 
); 

$terms = get_terms($taxonomies, $args);

You could also use 'child_of' instead;

Note: the difference between child_of and parent is that where parent only gets direct children of the parent term (ie: 1 level down), child_of gets all descendants (as many levels as are available)

Codex: get_terms

7
votes

Here is the Simplest way to get child category from specific parent

$parent_id = 12;
$termchildren = get_terms('product_cat',array('child_of' => $parent_id));