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.

Leave a Reply

7ads6x98y