SlightlyAmiss
Member
Posted 1 year ago #
Hello,
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:
<ul>
<?php wp_list_categories('orderby=id&show_count=1&use_desc_for_title=0&child_of=8'); ?>
</ul>
Is there some way to modify this code to display the subcat IDs instead of their names?
Any help would be greatly appreciated!
Thanks!
SlightlyAmiss
Member
Posted 1 year ago #
The following block of code gets me a lot closer to where I want to be:
<?php
$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?
Thanks!
SlightlyAmiss
Member
Posted 1 year 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;
endif;
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!