制作子类别下拉列表

时间:2016-03-19 06:09:11

标签: php wordpress

我想为子类别制作下拉列表,我有这段代码,但它没有工作......

       <select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo attribute_escape(__('Select Event')); ?></option>
<?php
$categories= get_categories('child_of=3');
foreach ($categories as $cat) {
$option = '<option value="/category/archive/'.$cat->category_nicename.'">';
$option .= $cat->cat_name;
$option .= ' ('.$cat->category_count.')';
$option .= '</option>';
echo $option;
}
?>
</select>

2 个答案:

答案 0 :(得分:0)

参数必须使用数组

$categories = get_categories( array( 'child_of' => 10 );

而不是

$categories= get_categories('child_of=3');

答案 1 :(得分:0)

谢谢,但它没有用,我找到了另一个解决方案

<li id="categories"><h2><?php _e( 'category-name' ); ?></h2>
    <?php wp_dropdown_categories('include=3,10' ); ?>
    <script type="text/javascript">
        <!--
        var dropdown = document.getElementById("cat");
        function onCatChange() {
            if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
                location.href = "<?php echo esc_url( home_url( '/' ) ); ?>?cat="+dropdown.options[dropdown.selectedIndex].value;
            }
        }
        dropdown.onchange = onCatChange;
        -->
    </script>
</li>