我在一个WP站点中有3个分层(类)自定义分类,books
, characters
和writers
. 我想在站点的侧栏中为每个术语创建一个动态填充的下拉菜单,以便在任何下拉菜单中选择术语时,站点自动加载相应的存档。
使用所述方法here 我只能在一种分类法上使用它。我一加上更多,就发现只有最后一个有效。
这是我到目前为止的情况。显然,我需要找到一种方法,使函数的后续实例不会超过早期实例。我认为更改ID和函数名可以做到这一点,但显然没有那么简单。
<?php
$writers = get_categories(\'taxonomy=writers\');
$select = "<select name=\'writers\' id=\'writers\' class=\'postform\'>n";
$select.= "<option value=\'-1\'>Writers</option>n";
foreach($writers as $writer){
if($writer->count > 0){
$select.= "<option value=\'".$writer->slug."\'>".$writer->name."</option>";
}
}
$select.= "</select>";
echo $select;
?>
<?php
$characters = get_categories(\'taxonomy=characters\');
$select = "<select name=\'characters\' id=\'characters\' class=\'postform\'>n";
$select.= "<option value=\'-1\'>Characters</option>n";
foreach($characters as $character){
if($character->count > 0){
$select.= "<option value=\'".$character->slug."\'>".$character->name."</option>";
}
}
$select.= "</select>";
echo $select;
?>
<?php
$books = get_categories(\'taxonomy=books\');
$select = "<select name=\'books\' id=\'books\' class=\'postform\'>n";
$select.= "<option value=\'-1\'>Books</option>n";
foreach($books as $book){
if($book->count > 0){
$select.= "<option value=\'".$book->slug."\'>".$book->name."</option>";
}
}
$select.= "</select>";
echo $select;
?>
JS公司:
<script>
var dropdown = document.getElementById("writers");
function onWriterChange() {
if ( dropdown.options[dropdown.selectedIndex].value != -1 ) {
location.href = "<?php echo home_url();?>/writer/"+dropdown.options[dropdown.selectedIndex].value+"/";
}
}
dropdown.onchange = onWriterChange;
var dropdown = document.getElementById("characters");
function onCharacterChange() {
if ( dropdown.options[dropdown.selectedIndex].value != -1 ) {
location.href = "<?php echo home_url();?>/character/"+dropdown.options[dropdown.selectedIndex].value+"/";
}
}
dropdown.onchange = onCharacterChange;
var dropdown = document.getElementById("books");
function onBookChange() {
if ( dropdown.options[dropdown.selectedIndex].value != -1 ) {
location.href = "<?php echo home_url();?>/book/"+dropdown.options[dropdown.selectedIndex].value+"/";
}
}
dropdown.onchange = onBookChange;
</script>
非常感谢您的帮助。