I have two arrays that have a parent category and sub category each appear in a select list, how do I make the sub category show items only from its parent category?
<?php $carMakes = array(
'show_option_all' => '',
'show_option_none' => ('All Makes'),
'orderby' => 'ID',
'order' => 'ASC',
'show_count' => 0,
'hide_empty' => 1,
'child_of' => 25,
'exclude' => 0,
'echo' => 1,
'selected' => 0,
'hierarchical' => 0,
'name' => 'cat',
'id' => '',
'class' => 'postform',
'depth' => 0,
'tab_index' => 0,
'taxonomy' => 'category',
'hide_if_empty' => false
); ?>
<?php $carModels = array(
'name' => 'subcat',
'hierarchical' => 1,
'parent' => get_cat_id('model'),
'show_option_none' => ('All Models'),
'hide_empty' => 0 );
?>
<?php wp_dropdown_categories($carMakes); ?>
<?php wp_dropdown_categories($carModels); ?>
need to only show car models that belong to car makes for example
Make=Toyota Model=Supra
Model=Corolla
Model=Tundra
Here is an example of the category structure
Make (parent category)
-Toyota
-Nissan
-Mazda
-Ford
Model (parent category)
-Supra
-Skyline
-Mustang
-Rx7
-Corolla
Always wanted to do an exercise on chained selections using Ajax, so, here we go 😉
This is a full plugin and should be installed in
wp-content/plugins/your-plugin-name
folder. Consists of three files, the plugin itself, the Javascript file and the Ajax loader image.Install the plugin and activate, and insert the following in some theme template file:
Also, adjust the two calls to
wp_dropdown_categories
as desired. Check the code comments for details.The sub-categories dropdown is modified in response to changes in the categories dropdown:
chained-categories.php
ajax.js
ajax-loader.gif
Why not use objects?
You need a factory to make cars.
a great reference: http://sourcemaking.com/creational_patterns
I also like to think about keep objects small, simple, and to do as little as possible. Break functions down to simple concepts like “make” and “show”. That makes them interchangable and extensible. You will eventually be able to just ask for $this->model->
I would approach like this:
1 object to organize the data //model
another to build your rows//controller
another to display //view
To start looking at it like that, write some functions first to gain an understanding of what you are wanting to know.
You may discover you need to query the data differently… In other words, ask the db a more specific question up front rather than filter it after you’ve received it. Maybe filtering now seems quicker, but how many other questions and filtering will you have to do later?
One more comment: php is more controllerish and javascript feels more viewish. I say solve the problems in their most appropriate and simplest context- stick with php on this issue.
The only way to do this without AJAX is to get a list of all your “Make” categories, then generate a dropdown for each “Model” of each “Make” using wp_dropdown_categories() with the child_of parameter. Hide all the “Make” dropdowns upon page load, attach a change event handler to the “Make” dropdown and when it’s called, show the appropriate “Model” dropdown while hiding all the rest. The showing/hiding can be done with jQuery or pure JS. Each “Model” dropdown will have to have a unique ID that can be used to identify which “Make” it belongs to.