从WP_Term_Query中获取terms的子项



我是WordPress新手,想获取所有子条目

下面是我的术语查询代码片段,在屏幕截图中显示了类别结构。

但是我从日志中得到的,它会显示所有的项。我也试过"parent",但不行。

(Version WordPress 6.0)

$term_args = [
'taxonomy'              => 'category',
'hide_empty'            => 0,
'hierarchical'          => 1,
'child_of'              => 1
];
$terms = new WP_Term_Query($term_args);
if (!is_admin()){
var_dump($terms->get_terms());
}

输入图片描述

使用get_terms。下面的代码获取父项的所有子项。

$args = array(
'taxonomy'              => 'category',
'hide_empty'            => 0,
'hierarchical'          => 1,
'child_of'              => 1
);
$taxonomies = get_terms( $args );
if ( !empty( $taxonomies ) || !is_wp_error( $taxonomies ) ) {
var_dump($taxonomies);
}

在我这端有效!

最新更新