How to Create a Simple Option to Select Categories in WordPress Customizer

WordPress supports the most common form fields for settings in the Customizer, such as text fields, checkboxes, selects, color pickers or media uploads. If the standard types for options are not sufficient, you can create your own very simple customizer controls by deriving from the WP_Customize_Control class.

A popular setting for magazine themes is the selection of a category for displaying articles in a featured content area. A separate control can take over the generation of the dropdown select and thus simplify the creation of category selection options in the theme.

Create Category Dropdown Control

With this short snippet of code you can create your own control to select a category:

It is important to prefix the newly created class to avoid conflicts with other plugins. Otherwise we only need the function render_content() to display the options.

With get_categories() we get all the categories of the page. The required HTML code is then generated in a foreach loop. The WordPress function selected() is used to determine the currently selected category.

Create option with new control

The new Category Dropdown Control can then be used. The following code creates a new featured content section in the customizer, which contains a setting for selecting a featured category:

This code example can of course also be modified to create your own customizer controls for the selection of posts, pages, tags or other custom post types and custom taxonomies.