This is just a quick code snippet that may help at least one person. I know it will. Earlier today I was looking over a theme and it was driving me crazy to see them re-inventing the wheel. A simple dropdown that core uses in order to display the categories. The way core uses choices is an associative array. The array key is the $value and the array value is used for the label. What does this mean?

It means that often times you have to flip things around because core can get certain values in a different manner. A great example is the get_page_templates function. The following is one way of creating this array.

// we get all the available templates
$templates = array_flip( get_page_templates() );

// new array is
$templates = 
	array (
	'templates/home.php' => 'home',
	'templates/featured.php' => 'featured posts',
	'templates/widgetized.php' => 'Widgetized Page'

Do keep in mind that is just for that particular function. There are several other functions that return either arrays or objects, so it is in your best interest to learn how and what each function returns data. I say this because the above code uses a basic PHP function in order to re-create the array. The function itself returns things the other way around. The codex page does a good job of explaining it.

In this post we will go over the get_categories function. It returns an array but not the way we want in order to use in the customizer. In order to do so we have to get a little creative in building the array.

// create an empty array
$cats = array();

// we loop over the categories and set the names and
// labels we need
foreach ( get_categories() as $categories => $category ){
	$cats[$category->term_id] = $category->name;

// we register our new setting
$wp_customize->add_setting( 'cats_elect', array(
	'default' => 1,
	'sanitize_callback' => 'absint'
) );

// we create our control for the setting
$wp_customize->add_control( 'cat_contlr', array(
	'settings' => 'cats_elect',
	'type' => 'select',
	'choices' => $cats,
	'section' => 'my_section',  // depending on where you want it to be
) );

This will return an integer for the category. Hence the absint sanitation callback. From there all we would have to do to get our setting is use:

$cat_id = intval( get_theme_mod( 'cats_elect', 1 ) );

You’ll notice I’ve used intval around the get_theme_mod function, that is so we make sure that we are getting an integer from our saved setting just in case. You can never be too careful, right?

So, there you have it! Just a few lines of code and we have a dropdown of our categories.


3 responses to “Customizer: dropdown category selection”

  1. Thanks so much, this has really saved me a headache!

  2. Hi Jose, What would I have to do to add an “all” option in the category dropdown? So that blog post from all category will be selected and shown in the front page ?

    1. Hi Ashiq!

      Would you mind expanding a little more on what you mean by that? Are you wanting to show one post from every category?