I'm trying to create a drop down that has the child categories of a specific category as options.

This is my PHP:

$args = array('child_of' => 88);
$categories = get_categories( $args );
foreach($categories as $category) { 
    echo $category->name;
    echo "<option value='$category->name'>
                            $category->name</option>";
}

For some reason it's not working. I'm not even getting an error on the front-end.

  • There's nothing wrong with your code, which is why you're not getting an error. But if there is no child category of category 88 then the returned result will be an empty array. Double check the ID for your 'child_of' value. (Of course, all that assumes that where you're using it is OK - I'm just talking about the code you posted.) – butlerblog Dec 1 at 0:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.