获取所选术语的列表选择选项下拉菜单
问题描述:
您能帮助我吗!
使用我的wordpress
实施例: 我有分类学:部门获取所选术语的列表选择选项下拉菜单
- 期限有职位名称:心脏诊所(医生1,医生2,医生3)
- 期限有职位名称:妇科门诊(医生4,医生5)
我想
当我选择心脏诊所则显示下拉
<select>
<option>doctor 1</option>
<option>doctor 2</option>
<option>doctor 3</option>
</select>
和
当我选择妇科门诊则显示下拉
<select>
<option>doctor 4</option>
<option>doctor 5</option>
</select>
太谢谢你了!
答
$post_type = 'event-posts';
$customPostTaxonomies = get_object_taxonomies($post_type);
if(count($customPostTaxonomies) > 0)
{
foreach($customPostTaxonomies as $tax)
{
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'taxonomy' => $tax,
'title_li' => ''
);
wp_dropdown_categories($args);
}
}
答
我知道你有2个选择元素。第一:部门和第二:医生。您的代码结构与以下内容相同:
<select id="dep" name="dep">
<option value="">Select Department</option>
<option value="?department={DEP_ID}" selected="selected">Cardiac Clinic</option>
<option value="?department={DEP_ID2}" selected="selected">Gynaecological Clinic</option>
</select>
并选择使用GET方法自动提交表单。你可以让它成为这个jQuery: 当然,你必须有jQuery库。
<script>
$(function(){
$('#dep').bind('change', function() {
var url = $(this).val();
if (url) {
window.location = url;
}
return false;
});
});
</script>
和编辑这个页面的PHP源与此相同:
$list = array();
if(isset($_GET['department'])){
// get your posts by term
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'department',
'field' => 'id',
'terms' => $_GET['dep']
)
)
);
$postslist = get_posts($args);
foreach($postslist as $doctor)
$list[] = array("ID" => $doctor->ID, "title" => $doctor->post_title);
// add doctors to list
} else {
$list[] = array("ID" => 0, "title" => "Select department first");
}
感谢SARI,但我想在1页第2选择选项。当我选择“部署选项”时,“医生选项”会改变。你明白吗? – 2014-12-04 06:40:13
是的,我的代码和方法。 – 2014-12-04 06:41:35