多个自定义分类下拉列表

时间:2021-09-12 作者:mtm

我在一个WP站点中有3个分层(类)自定义分类,books, characterswriters. 我想在站点的侧栏中为每个术语创建一个动态填充的下拉菜单,以便在任何下拉菜单中选择术语时,站点自动加载相应的存档。

使用所述方法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>
非常感谢您的帮助。

1 个回复
SO网友:mtm

“The”;下拉列表“;在这些函数中声明和使用的元素都是相同的,因此最后一个元素将覆盖所有之前的元素。

每次指定不同的术语可以解决此问题:

var writerdropdown = document.getElementById("writers");
function onWriterChange() {
    if ( writerdropdown.options[writerdropdown.selectedIndex].value != -1 ) {
    location.href = "<?php echo home_url();?>/writer/"+writerdropdown.options[writerdropdown.selectedIndex].value+"/";
    }
}
writerdropdown.onchange = onWriterChange;

var characterdropdown = document.getElementById("characters");
function onCharacterChange() {
    if ( characterdropdown.options[characterdropdown.selectedIndex].value != -1 ) {
    location.href = "<?php echo home_url();?>/character/"+characterdropdown.options[characterdropdown.selectedIndex].value+"/";
    }
}
characterdropdown.onchange = onCharacterChange;

var bookdropdown = document.getElementById("books");
function onBookChange() {
    if ( bookdropdown.options[bookdropdown.selectedIndex].value != -1 ) {
    location.href = "<?php echo home_url();?>/book/"+bookdropdown.options[bookdropdown.selectedIndex].value+"/";
    }
}
bookdropdown.onchange = onBookChange;

相关推荐

无法在前端显示来自metabox wp_dropdown_ages()的选定帖子标题

我在admin中使用wp_dropdown_pages() 显示自定义帖子类型中的帖子列表workshop. metabox下拉列表在admin中运行良好,我可以选择文章标题并保存它。问题是,当我尝试在前端显示metabox的值时,它只返回所选帖子的ID,而不是标题。如何在前端返回所选帖子的标题?metabox的PHP代码:add_action( \'add_meta_boxes\', \'mysite_work_add_meta_box\' ); if ( ! function_exists(