• Hello, I have two-three instances of a category dropdown but only the first instance on the page works. The other two drop down but don’t connect to the selected category page.
    The first instance, I hard-coded into the page.php template (rather than trying to figure out how to add a widget to a template – I got lost attempting this). The other two instances are widgets. Even if I remove the code from the template, only one of the widgetized dropdowns links to the category page.
    I have no idea how to register two or more of them. I tried using the code provided on this page: https://www.ads-software.com/support/topic/multiple-instances-of-wp-dropdpwn_categories-not-working-any-advice?replies=10

    but it put 3 instances of the dropdown in the theme file only and the two other dropdowns drop down but don’t link to the category pages.
    Here’s the page i’m working on.

    Thanks for any help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    With multiple dropdowns, the resulting query string upon submit would look like this:
    ?cat=5&cat=14&cat=9

    When PHP parses this into the $_POST array, each subsequent cat query var overwrites the previous one, thus only the final value is used to construct the post query.

    You need to alter the name of each dropdown so they are in the array form like what happens with a multiple select field – “cat[]”. The resulting query string would then look like this:
    ?cat[]=5&cat[]=14&cat[]=9

    Then all selected categories are placed in an array and will be represented in the resulting query.

    That said, why not offer a multiple select category list? Yes, there is no convenient function call to build it, but it’s not too much coding to get the categories with get_categories() and loop through the results to build the multiple select field.

    Thread Starter juliedc

    (@juliedc)

    Hi BC, Thank you for this. This makes sense to me except that I’m not sure where this would happen. One instance (the first one) is in the page.php template file, one is post content as built with a page builder block which I believe is a widget. The third instance is a widget with “bottom” placement, aka placed above the footer.

    They don’t need to be able to select multiple categories, it’s more of a UI issue in that it needs to be seen wherever they are on the home page.

    Moderator bcworkz

    (@bcworkz)

    Uh, never mind? XD Sorry, I’ve misinterpreted what your page’s configuration is. Now I’m understanding these dropdowns all are supposed to work independently of each other, they are all essentially copies of each other, but all on the same page. I hope I have it right this time.

    The behavior you describe does sound like a misconfigured multiple select setup. Please confirm that these dropdowns are NOT all in the same <form> block. They should each have their own. And I was not entirely joking about “never mind”, forget about the array form and square brackets [], they do not apply to your configuration. (maybe it’ll help someone else finding this thread ?? )

    A more likely related possibility is the javascript that submits the request (if that’s what the configuration is, more on this below) is picking up all the dropdowns at once because they all have the same ID, name, or whatever javascript is using to pick up the onchange action. Check the ID attribute for the dropdowns. They all must be different (javascript or not). But once the IDs are different, there needs to be separate event triggers to pick up each different field’s onchange event.

    The javascript issue may not apply to your configuration. If the page request is sent immediately upon selection, then javascript or jQuery is at work. If there’s a submit button or something that initiates the page request, then javascript may or may not be at work. Without javascript, the single form block is the only possible explanation that comes to mind.

    If javascript or jQuery is involved, then that script would need to be modified along with the ID or name of the dropdown. How that is done could be quite obvious or incredibly obscure. If the script is too obscure, it may be easier to either add a small submit (Go?) button and forget javasript, or rewrite the event handler.

    Furthermore, if the script is picking up on the field name instead of ID, changing field names will require some sort of rewrite rules to cause the new names to be put back to the “cat” query var that WP needs. A script rewrite is probably preferable.

    There could be another explanation, such as plugin or naming conflicts, but the above seem most likely. If you can investigate exactly how these forms are working, either a solution will present itself or we’ll know we need to focus elsewhere. Let us know what you can find out.

    This post has grown quite long ?? It’s really not that complicated, just hard to explain, compounded by multiple possibilities and knock on effects. Don’t worry, we’ll work through it and find a solution.

    Thread Starter juliedc

    (@juliedc)

    Thank you again, BC! I have made an executive decision to forego the 2nd instance that uses the page builder. The first instance only works if I also have the dropdown list in the “bottom”/footer area. I replaced the instance that was part of the page builder template with a table of category links – plain old html code. It works and I have to do it so I can move on to getting this thing live. I really appreciate your assistance!!!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Multiple instances of dropdown categories’ is closed to new replies.