The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

[resolved] Delimited List of Sub-Cat IDs? (3 posts)

  1. SlightlyAmiss
    Posted 5 years ago #


    I have been picking my brain (and the web) to come up with code that can generate a list of sub-category IDs, delimited by commas. I've tried so many things that it's hard to provide the code example that's gotten me the closest.

    However, the follow code generates a list of the child categories within parent ID 29:

    <?php wp_list_categories('orderby=id&show_count=1&use_desc_for_title=0&child_of=8'); ?>

    Is there some way to modify this code to display the subcat IDs instead of their names?

    Any help would be greatly appreciated!


  2. SlightlyAmiss
    Posted 5 years ago #

    The following block of code gets me a lot closer to where I want to be:

    $category_ids = get_all_category_ids();
    foreach($category_ids as $cat_id) {
      $cat_name = get_cat_name($cat_id);
      echo $cat_id . ', ';

    Is there some way to stipulate that I only want the category IDs to be children of parent cat, in my case 31?


  3. SlightlyAmiss
    Posted 5 years ago #

    This was extremely helpful on a per-post basis. Not quite what I was looking for but will get me to my goal.

    <?php $parentcat = get_category_by_slug('system');
    foreach((get_the_category()) as $childcat):
      if (cat_is_ancestor_of($parentcat, $childcat)):
        echo $childcat->cat_ID;
    endforeach; ?>

    I use multiple categories for every post, with this code I can input the parents slug in the first line and it will echo back out the corresponding sub category ID.

    Hope this can help someone!

Topic Closed

This topic has been closed to new replies.

About this Topic