转自:Wopus中文平台
可以使用函数:
1 | < ?php wp_dropdown_categories(); ?> |
该函数缺省用法为:
1 2 3 4 | $defaults = array('show_option_all' => '', 'show_option_none' => '', 'orderby' => 'ID', 'order' => 'ASC', 'show_last_update' => 0, 'show_count' => 0, 'hide_empty' => 1, 'child_of' => 0, 'exclude' => '', 'echo' => 1, 'selected' => 0, 'hierarchical' => 0, 'name' => 'cat', 'class' => 'postform', 'depth' => 0); |
给几个使用实例:
1. Dropdown with Submit Button
1 2 3 4 5 6 7 | <li id="categories">
<h2>< ?php _e('Categories:'); ?></h2>
<form action="<?php bloginfo('url'); ?>" method="get">
< ?php wp_dropdown_categories('show_count=1&hierarchical=1'); ?>
<input type="submit" name="submit" value="view" />
</form>
</li> |
2. Dropdown without a Submit Button using JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 | <li id="categories"><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 = "< ?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value; } } dropdown.onchange = onCatChange; --></script> </li> |
3. Dropdown without a Submit Button using JavaScript (2)
1 2 3 4 5 6 7 8 9 10 11 | <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>
</select></form>
</li> |
PS: 第一个实例可以显示二级分类,不过需要点击按钮才能进入分类,不太方便,后面两种不能显示二级分类,不过JS太多,载入就慢了
Tags: 下拉菜单, 分类, wordpress
固定链接:Wordpress侧栏中使用下拉菜单显示分类














