要在WordPress中调用指定分类ID目录下的所有子分类目录ID,你可以使用以下代码:

function get_child_categories_ids($cate_id) {
// 获取指定分类对象
$category = get_term_by(‘id’, $cate_id, ‘category’);

// 检查分类是否存在
if (is_wp_error($category) || is_null($category)) {
    return array();
}

// 获取子分类数组
$child_categories = get_categories(array(
    'parent' => $cate_id,
    'taxonomy' => 'category',
));

// 提取子分类ID
$child_cate_ids = array();
foreach ($child_categories as $child_category) {
    $child_cate_ids[] = $child_category->term_id;
}

return $child_cate_ids;

}

// 使用示例:获取指定分类ID(例如:5)下的所有子分类ID
s p e c i f i e d c a t e i d = 5 ; specified_cate_id = 5; specifiedcateid=5;child_cate_ids = get_child_categories_ids($specified_cate_id);

// 输出结果
echo ‘子分类ID:’;
foreach ($child_cate_ids as $child_cate_id) {
echo $child_cate_id . ’ ';
}

将以上代码添加到WordPress主题中的functions.php文件中。然后,你可以在需要的地方调用get_child_categories_ids()函数并传入指定的分类ID,以获取其所有子分类的ID。在上面的示例中,我们使用了分类ID为5的情况。如果你想使用其他分类ID,只需更改$specified_cate_id的值即可。

原文

 https://www.jianzhanpress.com/?p=7126