WordPress wp_dropdown_categories() Function

Definition
In WordPress, wp_dropdown_categories() builds an HTML select box of categories. Visitors can pick one category from the list.
It prints the select box by default and also returns the HTML string. Set echo to 0 if you only want the string.
Syntax
Pass an array of arguments. Every argument is optional.
wp_dropdown_categories();
wp_dropdown_categories( array(
'show_option_all' => 'All Categories',
'name' => 'cat',
) );
| Call | What it does |
|---|
wp_dropdown_categories() | Prints a select box with the default settings. |
wp_dropdown_categories( array( 'show_option_all' => 'All Categories' ) ) | Adds a first option for all categories. |
$html = wp_dropdown_categories( array( 'echo' => 0 ) ) | Returns the HTML instead of printing it. |
Put the select box inside a form so the choice can be sent.
Full declaration
wp_dropdown_categories( array|string $args = '' ): string
Parameters
| Parameter | Description |
|---|
show_option_all | Optional. Text for a first option that means all categories. Default is empty. |
name | Optional. The name of the select field. Default is cat. |
selected | Optional. The ID of the category that is selected. Default is 0. |
hierarchical | Optional. 1 shows child categories under their parents. Default is 0. |
orderby | Optional. Sort by id, name, count, and more. Default is id. |
hide_empty | Optional. 1 hides categories without posts. Default is 1. |
taxonomy | Optional. The taxonomy to list. Default is category. |
echo | Optional. 1 prints the HTML. 0 does not print it. Default is 1. |
Return Value
| Return Value | Description |
|---|
| string | The HTML of the select box. It is also printed when echo is 1. |
WordPress Examples
Three short theme examples. Each one has a sample output.
Example 1 — Basic drop-down
Print a select box with an All Categories option.
<?php
wp_dropdown_categories( array(
'show_option_all' => 'All Categories',
) );
?>
<select name='cat' id='cat' class='postform'>
<option value='0'>All Categories</option>
<option class="level-0" value="3">News</option>
<option class="level-0" value="5">Tips</option>
</select>
How It Works
show_option_all adds the first option.- Each category becomes an
option with its ID as the value. - The select box is printed because
echo is on by default.
Example 2 — In a form
Send the visitor to the chosen category.
<form action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
<?php
wp_dropdown_categories( array(
'show_option_all' => 'Choose a category',
'name' => 'cat',
) );
?>
<button type="submit">Go</button>
</form>
How It Works
- The form sends a
GET request to the home page. - The field name
cat is a built-in WordPress query variable. - WordPress opens the category archive for the chosen ID.
Example 3 — Return the HTML
Save the select box in a variable and pre-select a category.
<?php
$dropdown = wp_dropdown_categories( array(
'echo' => 0,
'selected' => 3,
) );
echo '<p>' . $dropdown . '</p>';
?>
<p><select name='cat' id='cat' class='postform'>
<option class="level-0" value="3" selected="selected">News</option>
<option class="level-0" value="5">Tips</option>
</select>
</p>
How It Works
'echo' => 0 returns the HTML and does not print it.'selected' => 3 marks category 3 as selected.- You can now wrap the HTML in your own tags.
Frequently Asked Questions
It builds an HTML select box (drop-down) that lists categories.
It does both by default. Set echo to 0 to only return the HTML.
Use the show_option_all argument, for example 'show_option_all' => 'All Categories'.
By default hide_empty is on. Categories with no posts are hidden. Use 'hide_empty' => 0 to show them.
Yes. Use the taxonomy argument, for example 'taxonomy' => 'post_tag'. Set 'name' to match the query variable you need.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful