Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So, Im trying to get a chained select to work to select a state then a city with that state. (2 fields).

Here is what Ive got:

First my function to create the menu using get_categories().

First problem: Not sure what to put for $child to get it to only show child categories.

function taxonomy_dropdown($dropID, $exclude, $taxonomy, $parent, $child) {
    $args = array(
        'type'                     => 'post',
        'child_of'                 => $child,
        'parent'                   => $parent, 
        'orderby'                  => 'name',
        'order'                    => 'ASC',
        'hide_empty'               => 0,
        'hierarchical'             => 1,
        'exclude'                  => $exclude,
        'include'                  => '',
        'number'                   => '',
        'taxonomy'                 => $taxonomy,
        'pad_counts'               => false
    );

    $categories = get_categories( $args );

    $menu = '<select name="'.$dropID.'" id="'.$dropID.'" class="postform">';

    foreach($categories as $category) {
        $menu .= '<option class="" value="'.$category->name.'">'.$category->name.'</option>';
    }   
    $menu .= '</select>';
    echo $menu;
}

In my plugin, the function is called like so:

<? 
    taxonomy_dropdown('state','','states_category','0'); 
    taxonomy_dropdown('city','','states_category','','1');
?>

The second one for 'city' doesnt work because of the above problem of I cant get it to only get the child categories.

So once I get that to work, I should have 2 selects, one showing 'states' and one showing 'cities.'

Now here is where I get lost. I want to use chained.js (http://www.appelsiini.net/projects/chained) to turn these two select menus into a chained select.

Im pretty sure Ive enqued it into my plugin properly:

function includeChained() {
    wp_enqueue_script(
        'chained',
        plugins_url( '/js/chained/chained.min.js' , __FILE__ ),
        array( 'jquery' )
    );
}
add_action( 'wp_enqueue_scripts', 'includeChained' );

Now Im totally clueless about how to use jquery and javascript beyond the ultra basics. I tried what the docs show for using the class based usage:

<script>
$("#state").chained("#city");
</script>

But nothing happens. If anybody could help I would be greatly appreciative!

Thanks!!

share|improve this question
    
Can you get it working in principle on proof of concept, sans WordPress? Live page, demonstrating the issue would probably be helpful as well. –  Rarst Nov 22 '13 at 10:29
    
Ok, Ive got it working now: weedstarter.com/add-location -- Ive got 2 drop downs, one showing parent categories only and one showing child categories only. From what Ive figured out about how to use chained.js, all I need to do to get it to work is to get the class of the child category options to be the name of the parent category and it should work. –  Jon Furry Nov 22 '13 at 20:48
    
@JonFurry Please add that as separate answer. You can mark it as solution just two days after that. We'd appreciate that. –  kaiser Feb 11 at 0:48

1 Answer 1

Code is here: Wordpress chained categories

I write the same code for categories. You can edit for your needs. I used the that js plugin: http://www.appelsiini.net/projects/chained

share|improve this answer
    
Please explain why this answer is helpful. Answers should be more than plain links. They should actually be an answer instead of a route where someone will maybe find an answer. Please help preventing link rot, edit your answer and provide the needed information that helps the OP as well as later visitors to solve their problem. –  kaiser Apr 10 at 17:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.