Codex tools: Log in / create account
Contents |
Returns an array of category objects matching the query parameters.
Arguments are pretty much the same as wp_list_categories and can be passed as either array or in query syntax.
<?php $categories = get_categories(parameters); ?>
<?php $defaults = array('type' => 'post',
'child_of' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'include_last_update_time' => false,
'hierarchical' => 1,
'exclude' => ,
'include' => ,
'number' => ,
'pad_counts' => false);?>
Here's how to create a dropdown box of the subcategories of, say, a category that archives information on past events. This mirrors the example of the dropdown example of wp_get_archives which shows how to create a dropdown box for monthly archives.
Suppose the category whose subcategories you want to show is category 10, and that its category "nicename" is "archives".
<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo attribute_escape(__('Select Event')); ?></option>
<?php
$categories= get_categories('child_of=10');
foreach ($categories as $cat) {
$option = '<option value="/category/archives/'.$cat->category_nicename.'">';
$option .= $cat->cat_name;
$option .= ' ('.$cat->category_count.')';
$option .= '</option>';
echo $option;
}
?>
</select>