admin管理员组文章数量:1415111
Alright, so first, I need you to check out this wire-frame so you can see what I'm trying to accomplish:
What I've done so far is this:
- I created a custom post type for individual addresses.
- I created a hierarchical custom taxonomy where the "parent categories" are States, and the "child categories" are Cities.
I've used the following code to populate the first select element:
<select name="states" id="states" class="etc"> <?php $terms = get_terms('area', 'parent=0'); $count = count($terms); if ( $count > 0 ){ foreach ( $terms as $term ) { echo '<option value="' . $term->name . '">' . $term->name . '</option>'; } } ?> </select>
So here's what I need to know:
- How do I populate the second select element based on which parent is selected in the first?
- How do I use these select elements to filter my custom posts?
Alright, so first, I need you to check out this wire-frame so you can see what I'm trying to accomplish:
What I've done so far is this:
- I created a custom post type for individual addresses.
- I created a hierarchical custom taxonomy where the "parent categories" are States, and the "child categories" are Cities.
I've used the following code to populate the first select element:
<select name="states" id="states" class="etc"> <?php $terms = get_terms('area', 'parent=0'); $count = count($terms); if ( $count > 0 ){ foreach ( $terms as $term ) { echo '<option value="' . $term->name . '">' . $term->name . '</option>'; } } ?> </select>
So here's what I need to know:
- How do I populate the second select element based on which parent is selected in the first?
- How do I use these select elements to filter my custom posts?
1 Answer
Reset to default 1Are you familiar with jQuery? You'll want to post the term to a admin-ajax, and use the response to populate the second select.
Here is the codex documentation: http://codex.wordpress/AJAX_in_Plugins
A loose example for your situation:
$first_term = $('#states');
$name = $first_term.children('option:selected').text();
var data = {
action: 'tag_slug',
slug: $slug
};
var ajaxurl = 'wp-admin/admin-ajax.php';
$.post(ajaxurl, data, function(response) {
$('#city').empty().append(response);
});
You would build the options in a similar way as you have show in your AJAX response callback, so that that is what is returned and is what is appended to the City select.
Note the "Ajax on the Viewer-Facing Side" section of the above codex link to make sure it works for non-loggedin users.
本文标签: How can I populate a select element with terms from a custom taxonomy and filter post results
版权声明:本文标题:How can I populate a select element with terms from a custom taxonomy and filter post results? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745213586a2648030.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论