categories – how do I exclude a category foreach

Question

I have this code and would like to exclude a parent category with all subcategories associated with it, can you help me? I’ve been trying to fix it for a while but I can’t

    $cat_array = array();
            $categories = get_categories();
foreach($categories as $category) {
    $json_obj = new stdClass();
    $json_obj->id = $category->term_id;
    $json_obj->name = $category->name;
    $json_obj->slug = $category->slug;
    $json_obj->url = get_category_link($category->term_id);
    $cat_array[] = $json_obj;

}

This is the code and I can’t even if I insert the category to be excluded in the array, I also tried this way but I can’t:

        $cat_array = array('exclude_tree' => 38398);
                $categories = get_categories($cat_array);
    foreach($categories as $category) {
        $json_obj = new stdClass();
        $json_obj->id = $category->term_id;
        $json_obj->name = $category->name;
        $json_obj->slug = $category->slug;
        $json_obj->url = get_category_link($category->term_id);
        $cat_array[] = $json_obj;

    }

I’m a little desperate..

0
tony9031 2 weeks 2022-11-18T05:13:35-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse