phpwordpresscategorieschildren

Get slugs and ids of wordpress category children


I m getting the slug and id of one of the my-category children.

Not sure how to grab slugs and ids from all children.

$incat = get_category_by_slug("my-category");

    foreach((get_the_category()) as $inchild):

      if (cat_is_ancestor_of($incat, $inchild)):

         $my_category_name = strtolower( str_replace(" ", "-", $inchild->slug) );
         $my_category_id = $inchild->cat_ID;

      endif;

    endforeach;

Solution

  • You can use

    get_categories('child_of=X');

    where 'X' is your parent category name..

    It will return an array that can be manipulated through foreach.

    For more info you can checkout - http://codex.wordpress.org/Function_Reference/get_categories