• I’ve read all of the forum posts I have found having to do with wp_dropdown_categories and not found a mention of my issue.

    I need to use this tag twice in my sidebar. (once to list just homework posts, and again to list all the rest of the blog categories). It works in terms of what is presented to the user, but it fails to validate because there’s no way to set a unique ID for the form created by wp_dropdown_categories. You can’t have two forms on a page with the same ID and still validate xhtml transitional.

    The default ID and Name for wp_dropdown_categories is “cat”, one of which (ID or name) is what is needed to call the correct category, producing an url like:

    <http://fhuhs.org/departments/spanish/?cat=25&gt;

    The above url correctly displays the category, however, if I use the ‘name’ parameter, changing the name of the form, which _also_ changes the ID, then there’s a problem with getting the dropdown to work using the code found on the following page of the codex:

    <http://codex.wordpress.org/Template_Tags/wp_dropdown_categories&gt;

    <li id="categories">
    	<h2><?php _e('Posts by Category'); ?></h2>
    	<form action="<?php bloginfo('url'); ?>/" method="get">
    <?php
    	$select = wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0');
    	$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
    	echo $select;
    ?>
    	<noscript><input type="submit" value="View" /></noscript>
    	</form>
    </li>

    If the name is changed, the call no longer works.

    I may be confused as to whether or not it’s ‘name’ or ‘id’ that is used to generate the url in the code below, but changing ‘name’ causes the calls to fail.

    This code (using the default name) works:

    <!-- Homework -->
    <li>Spanish Homework:<ul><li><form action="<?php bloginfo('url'); ?>/" method="get">
    	<?php
    		$select = wp_dropdown_categories('child_of=13&show_option_none=Select your class&orderby=name&echo=0');
    		$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
    		echo $select;
    	?>
    	<noscript><input type="submit" value="View" /></noscript>
    	</form></li>
    </ul></li>

    But this code (changing the name property) fails:

    <!-- Homework -->
    <li>Spanish Homework:<ul><li><form action="<?php bloginfo('url'); ?>/" method="get">
    	<?php
    		$select = wp_dropdown_categories('name=hmwrk&child_of=13&show_option_none=Select your class&orderby=name&echo=0');
    		$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
    		echo $select;
    	?>
    	<noscript><input type="submit" value="View" /></noscript>
    	</form></li>
    </ul></li>

    The above code generates this url, which obviously won’t work to call a category:

    <http://fhuhs.org/departments/spanish/?hmwrk=25&gt;

    So, three questions:

    1. Is the WP 2.7 code using ‘id’ or ‘name’ to generate the url (obviously I need it generate ‘cat’ but I want to be able to change the id for each use of the tag to get my page to validate).
    2. Is there a workaround out there some where? For example, am I missing a parameter that would cause wp_dropdown_categories to generate permalinks instead of the ‘?cat=25’ url that it seems to use? Would I still be able to change just the ‘id’ of the form and thus be able to validate xhtml using the tag twice in my sidebar?
    3. Where in the WP 2.7 files is the code for wp_dropdown_categories if there is no solution or fix so I can hack it myself?

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter dherren

    (@dherren)

    Anyone? Bueler?

    Thread Starter dherren

    (@dherren)

    Well, no responses, but I’ve found a fix by modifying the core code.

    In /wp-includes/category-template.php I found the code for the wp_dropdown_categories. By modifying line 396:

    $output = "<select name='$name' id='$name' class='$class' $tab_index_attribute>\n";

    to this:

    $output = "<select name='$name' class='$class' $tab_index_attribute>\n";

    I can now use wp_dropdown_categories more than once in my sidebar and my pages once again validate xhtml 1.0 transitional.

    I can’t even begin to imagine what the wider ramifications might be to this, but given that I validate now and before I didn’t, and the dropdown categories are being selected correctly, I think it’s ok.

    Not sure if related, but this will at least interest you:
    http://trac.wordpress.org/ticket/8700

    @dherren: just ran into this same thing in 2.9, with a template I am tinkering on that uses multiple category dropdowns.

    You have a point, in XHTML one should not be using id= and name= in this fashion. In fact, according to the codex:

    In HTML it was legal to use ID and NAME attributes interchangeably. In XHTML the NAME attribute is formally deprecated and cannot be used. In all cases where you would think to use a NAME attribute you must now use ID instead.

    Or course, this does not solve the problem with the duplicate ID tags in one page, there should be a different way of generating ID’s for multiple selects in one page in order to make the page still validate, and not break every single theme out there that assumes that there will be only one category drop-down on a page, and then hooks a bunch of CSS to it.

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