Multiple Custom Taxonomy Dropdowns Lists

I have 3 hierarchical (category-like) custom taxonomies in a WP site, books, characters and writers. I want to create a dynamically populated dropdown menu of terms for each of these in the site's sidebar, so that on selecting a term in any of the dropdown the site automatically loads the corresponding archive.

Using the method described here I can get this working on one taxonomy only. As soon as I add more I find that only the last one works.

Here's what I have so far. Clearly I need to find a way for later instances of the function to not over-ride the earlier ones. I thought that changing the IDs and function names would do this but it's clearly not that simple.

?php
    $writers = get_categories('taxonomy=writers');
    $select = select name='writers' id='writers' class='postform'n;
    $select.= option value='-1'Writers/optionn;
    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/optionn;
    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/optionn;
    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

Many thanks for any help.

Topic dropdown select custom-taxonomy Wordpress

Category Web


The "dropdown" element declared and used in each of these functions is the same, so the last one is overriding all previous.

Specifying different terms each time fixes the problem:

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;

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.