A shortcode for using Category Ajax Chain Selects

Ajax Category Chain Selects

I was recently working on a project where I wanted to be able to quickly return a list of products based on the categories within a custom post type.

The idea is that the user could use drop-downs to drill-down in two category levels and return all of the products in the second tier category.

Category Ajax Chain Selects was the perfect solution to my needs. Using Ajax meant that users wouldn’t have to differentiate between two levels of categories in a single drop-down or endure a new page load just to get the second list of categories.

However, by default, the plugin only comes with a PHP function, so you need to edit your theme files to use it on your site.

I found this quite restrictive for my needs, so I built a shortcode for the function that allows you to display the chain selects anywhere you want, including on a post, page or sidebar widget.

The following shortcode has all of the parameters that the PHP function does. These are the parameters that are available:

  • tax – the taxonomy you’re trying to target
  • level – how many category levels deep you want to search
  • titles – the title in the drop-down for each category level, separated by a comma
  • labels – the label above each drop-down, separated by a comma
  • btn_text – the text to put in the “submit” button
  • exclude – the ID of any category you wish to exclude
  • count – whether to show how many posts in each category

If you drop that snippet into your functionality plugin, you’ll be able to put your chain selects anywhere you can put a shortcode.

3 thoughts on “A shortcode for using Category Ajax Chain Selects”

  1. Nikhil Chaudhary says:

    It works.!! Great.

  2. Juvan Nortje says:

    Hi there,

    Thank for the great post! I am a bit of a noob regarding php.

    I have included the snippet into my functions.php file which now looks like this:

    //Ajax chain select
    function chain_select_shortcode( $atts ) {
    extract( shortcode_atts( array( ‘tax’ => ‘category’, ‘level’ => 1, ‘titles’ => array( ‘Please select…’ ), ‘labels’ => ”, ‘btn_text’ => ‘Submit’, ‘exclude’ => ”, ‘count’ => 1 ), $atts ) );
    return chainselect_getcategories( $tax, $level, explode( ‘, ‘, $titles ), explode( ‘, ‘, $labels ), $btn_text, $exclude, $count );
    add_shortcode( ‘ajax_chain_select’, ‘chain_select_shortcode’ );

    And on my site, I’ve got the following shortcode:

    [ajax_chain_select tax=’wpbdp_category’ level=’1′ titles=’Countries,Provinces’ labels=’South Africa’ btn_text=’Search’ exclude=”” count=””]

    Nothing is displaying and I get the following message:
    “Category Chain Select Plugin
    Error :: Number of labels don’t match number of levels”

    Please give me a little advice!!!


    1. Your shortcode is confusing because you’ve specified titles for two levels:


      but only specified 1 level:


      and 1 label:

      labels='South Africa'

Leave a Reply