user3284079
user3284079

Reputation:

how to get sub categories by parent category id in wordpress?

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

Upvotes: 12

Views: 45903

Answers (2)

Nick M
Nick M

Reputation: 1667

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

Upvotes: 21

Muhammad Rehman
Muhammad Rehman

Reputation: 455

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));

Upvotes: 8

Related Questions