• I feel like I’m close on this. I think I have all the pieces of the puzzle; I just don’t know the correct syntax. Someone please help me out.

    I want to dynamically generate a list of category IDs of just the categories which are currently children of a certain parent category.

    I found these functions which I think will help:
    $category_ids = get_all_category_ids();
    Returns an array containing IDs for all categories.

    cat_is_ancestor_of(cat1, cat2);
    Returns true if cat1 is an ancestor of cat2.

    I’m thinking I want to loop thru the array of category ids generated by get_all_category_ids(), using cat_is_ancestor_of() to check to see which are children of “cat2” (the parent category). For the categories that are children of cat2, I want to [do some code].

    Is this right? How do I write this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter soundandshape

    (@soundandshape)

    a quick search of this forum reveals that others are trying to figure this one out, but with no resolution! can anyone help?

    I think I’m close– I just don’t know how to use the cat_is_ancestor_of(cat1, cat2) function properly. any tips?

    So far I’ve been fiddling around with something like this

    $category_ids = get_all_category_ids(); //puts array of category IDs into variable $category_ids
    foreach ($category_ids as $category) { //loop thru the array…
    if (cat_is_ancestor_of($category, 5)) { //determine which category IDs are children of category 5.
    [do some code;] // only if $category is a child of ID5
    }
    }

    this doesn’t work, but it illustrates what I’m trying to do. I’m hoping I just need the correct syntax. how do I write this properly?

    btw, I found this from Otto.
    https://www.tibbles.net/designs/?p=25
    It seems like it’s along the same lines. Does it spark any ideas from anyone?

    any help would be appreciated!

    Thread Starter soundandshape

    (@soundandshape)

    I had the same issue but found a way how to get it done:

    $the_cat = get_the_category();
    	foreach($the_cat as $category) {
    		$category = $category->cat_ID;
    		$category = array($category,'');
    			foreach($category as $cat){
    				if(cat_is_ancestor_of($parentid,$cat)) {
    // your code goes in here
    								}
    			}
    }

    Just replace $parentid with the ID you want to be the parent.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Child Category IDs–So Close! please help’ is closed to new replies.