WP: Creating Dropdown List of Categories
Jun 14th, 2008 | By dikiem | Category:WordPress Tips and Hack | no comments | 258 views |Here is a little WordPress tip / trick. For anyone who is trying to build their sidebar and or want to add a dropwdown list style for category listing, this is one simple method.
http://codex.wordpress.org/Template_Tags/wp_dropdown_categories
You can use the code snip they’ve provided
<li id="categories">lt;h2><?php _e('Posts by Category'); ?></h2>
<?php wp_dropdown_categories('show_option_none=Select category'); ?><script type="text/javascript"><!--
var dropdown = document.getElementById("cat");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
location.href = " ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
--></script>
</li>
Parameters
- show_option_all
- (string) Causes the HTML for the dropdown to allow you to select All of the categories.
- show_option_none
- (string) Causes the HTML for the dropdown to allow you to select NONE of the categories.
- orderby
- (string) Key to sort options by. Valid values:
- 'ID' (Default)
- 'name'
- order
- (string) Sort order for options. Valid values:
- 'ASC' (Default)
- 'DESC'
- show_last_update
- (boolean) Sets whether to display the date of the last post in each category. Valid values:
- 1 (True)
- 0 (False – Default)
- show_count
- (boolean) Sets whether to display a count of posts in each category. Valid values:
- 1 (True)
- 0 (False – Default)
- hide_empty
- (boolean) Sets whether to hide (not display) categories with no posts. Valid values:
- 1 (True – Default)
- 0 (False)
- child_of
- (integer) Only display categories that are children of the category identified by its ID. There is no default for this parameter.
- exclude
- (string) Comma separated list of category IDs to exclude. For example, ‘exclude=4,12′ means category IDs 4 and 12 will NOT be displayed/echoed or returned. Defaults to exclude nothing.
- echo
- (boolean) Display bookmarks (TRUE) or return them for use by PHP (FALSE). Defaults to TRUE.
- 1 (True – default)
- 0 (False)
- selected
- (integer) Category ID of the category to be ’selected’ or presented in the display box. Defaults to no category selected.
- hierarchical
- (boolean) Display categories in hierarchical fashion (child categories show indented). Defaults to FALSE.
- 1 (True)
- 0 (False – Default)
- name
- (string) Name assigned to the dropdown form. Defaults to ‘cat’.
- class
- (string) Class assinged to the dropdown form. Defaults to ‘postform’.
- depth
- (integer) This parameter controls how many levels in the hierarchy of Categories are to be included in the list of Categories. The default value is 0 (display all Categories and their children). This parameter added at Version 2.5
- 0 – All Categories and child Categories (Default).
- -1 – All Categories displayed in flat (no indent) form (overrides hierarchical).
- 1 – Show only top level Categories
n – Value of n (some number) specifies the depth (or level) to descend in displaying Categories
Hot Items From Amazon!
no comments | 258 views



