php 如何通过wordpress中的父类别ID获取子类别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22443352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:56:35  来源:igfitidea点击:

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

phpwordpresswordpress-theming

提问by Nick M

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

请,任何人都可以帮助我,我是 wordpress 世界的新手。 如何通过wordpress中的父类别ID获取子类别?

回答by Nick M

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

你需要使用 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;

你也可以'child_of'改用;

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)

注意:child_of 和 parent 之间的区别在于,parent 仅获取父项的直接子项(即:向下 1 级),child_of 获取所有后代(尽可能多的级别)

Codex: get_terms

法典:get_terms

回答by Muhammad Rehman

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