我在菜单中创建了一个带有下拉列表的目录网站,允许用户在整个类别和子类别中导航。我使用wp_dropdown_categories()来显示菜单。此处共享的javascript http://codex.wordpress.org/Function_Reference/wp_dropdown_categories允许用户选择类别并导航到该页面。当'所有类别'被选中,没有任何反应。如何添加该功能?
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$cat_name = $category->name;
$cat_id2 = get_category_by_slug($cats[2]);
$args = array(
'show_option_all' => 'All Categories',
'show_option_none' => 'BROWSE',
'orderby' => 'NAME',
'order' => 'ASC',
'show_count' => 0,
'hide_empty' => 1,
'child_of' => 0,
'exclude' => 1,
'echo' => 1,
'selected' => $cat_id2->term_id,
'hierarchical' => 1,
'name' => 'cat',
'id' => '',
'class' => 'postform',
'depth' => 1,
'tab_index' => 0,
'taxonomy' => 'category',
'hide_if_empty' => false,
);
wp_dropdown_categories( $args );
$cat_id3 = get_category_by_slug($cats[3]);
$args2 = array(
'show_option_all' => 'All ' . $cat_id2->name,
'show_option_none' => '',
'orderby' => 'NAME',
'order' => 'ASC',
'show_count' => 0,
'hide_empty' => 1,
'child_of' => $cat_id2->term_id,
'exclude' => 1,
'echo' => 1,
'selected' => $cat_id3->term_id,
'hierarchical' => 1,
'name' => 'cat2',
'id' => '',
'class' => 'postform',
'depth' => 1,
'tab_index' => 0,
'taxonomy' => 'category',
'hide_if_empty' => false,
);
//print $cats[2];
if(($cats[1] == "category") AND ($cats[2] != '')) { wp_dropdown_categories( $args2 ); }
$cat_id4 = get_category_by_slug($cats[4]);
$args3 = array(
'show_option_all' => '',
'show_option_none' => 'All ' . $cat_id3->name,
'orderby' => 'NAME',
'order' => 'ASC',
'show_count' => 0,
'hide_empty' => 1,
'child_of' => $cat_id3->term_id,
'exclude' => 1,
'echo' => 1,
'selected' => $cat_id4->term_id,
'hierarchical' => 1,
'name' => 'cat3',
'id' => '',
'class' => 'postform',
'depth' => 1,
'tab_index' => 0,
'taxonomy' => 'category',
'hide_if_empty' => false,
);
if($cats[3] != '') { wp_dropdown_categories( $args3 ); }
Javascript
<script type="text/javascript"><!--
var dropdown = document.getElementById("cat");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
location.href = "<?php echo get_option('home');
?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
--></script>
<script type="text/javascript"><!--
var dropdown2 = document.getElementById("cat2");
function onCatChange2() {
if ( dropdown2.options[dropdown2.selectedIndex].value > 0 ) {
location.href = "<?php echo get_option('home');
?>/?cat="+dropdown2.options[dropdown2.selectedIndex].value;
}
}
dropdown2.onchange = onCatChange2;
--></script>
<script type="text/javascript"><!--
var dropdown3 = document.getElementById("cat3");
function onCatChange3() {
if ( dropdown3.options[dropdown3.selectedIndex].value > 0 ) {
location.href = "<?php echo get_option('home');
?>/?cat="+dropdown3.options[dropdown3.selectedIndex].value;
}
}
dropdown3.onchange = onCatChange3;
--></script>