wpsl_dropdown_category_args

  1. Description
  2. Parameters
  3. Usage
  4. Changelog
  5. Source File
  6. Related

Description

This filter allows you to modify the arguments that are used to create the category dropdown.

You can, for example, use it to select a different default item, change the “any” text, and make it show the number of locations that each category holds after the category name.

These are the default values that are used:

Parameters

$args - (array) The default dropdown arguments.

Usage

This code example includes the post count after each category in the dropdown:

This one shows how to sort the category items by ID. So the oldest category will show first, and the latest one last:

If you want to change the text used for the default dropdown item, then the code below shows you how to do this:

You can also hide the default dropdown item completely with the code below.

Setting a different category as the selected one can be done with the code below:

Restrict the shown options on each page to a specific parent category:

Only show the child categories from a specific parent category on a page called ‘wpsl’:

add_filter( 'wpsl_dropdown_category_args', 'custom_dropdown_category_args' );

function custom_dropdown_category_args( $args ) {

    if ( is_page( 'wpsl' ) ) {
        $args['parent'] = 3; // parent category ID
        $args['hierarchical'] = true; // only show the child categories
    }

    return $args;
}

You can find the ID’s of your own categories on the Store Locator > Store Categories page. Open the category you want to set as selected and look for tag_ID in the url. The number behind it is the ID you should use it instead of 1 as in the example above.

You can see the full list of arguments you can use here.

Changelog

Since 2.2

Source File

This filter is located in /frontend/class-frontend.php

Placement

The code needs to be placed in the functions.php file inside your active theme folder.