Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

How to create a WordPress dropdown list using get_categories()

How to create a WordPress dropdown list using get_categories()

Wondering how to create a WordPress dropdown list ? In this article, we walk you through this process with code (using get_categories functions) given below. First we will create
a varibale $args and pass some parameters like hide_emty (1 will hide and 0 will display category with 0 post), similarly if you want to hide certain category
then pass category id in array like array(1,13).

Wondering how to create a WordPress dropdown list Code

<select name="category" id="category" class="form-control" >
<option value="">Select Category </option>
<?php
$args=array(
'hide_empty' => 1,
'exclude' => array(1,13), //hiding category id 1 and 13 ...
);
$categories = get_categories($args);
foreach ($categories as $category) {
//$option .= '<option value="'.get_option('home').'/category/'.$category->slug.'">';
$option .= '<option value="'.$category->slug.'">';
$option .= $category->cat_name;
//$option .= ' ('.$category->category_count.')';
$option .= '</option>';
}
echo $option;
?>
</select>

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of
Do you have any Query / Suggestion