我在这方面是个新手。到目前为止我所做的就是。我使用以下代码生成了一个包含所有第一级类别的元素:
 <?php
    $args = array(
        'show_option_all'    => 'Blog Categories',
        'show_option_none'   => '',
        'option_none_value'  => '-1',
        'orderby'            => 'ID',
        'order'              => 'ASC',
        'show_count'         => 0,
        'hide_empty'         => 0,
        'child_of'           => 0,
        'exclude'            => '',
        'include'            => '4,2,3,5,6',
        'echo'               => 1,
        'selected'           => 0,
        'hierarchical'       => 1,
        'name'               => 'cat',
        'id'                 => 'cat-blog',
        'class'              => 'form-control mt33',
        'depth'              => 0,
        'tab_index'          => 0,
        'taxonomy'           => 'category',
        'hide_if_empty'      => false,
        'value_field'        => 'term_id',
    );
    wp_dropdown_categories( $args );
    ?>但不幸的是,我无法设法将<a href="">添加到由wp_dropdown_categories函数生成的所有<option>标记中。
是否可以为每个生成的类别添加链接。我想将用户重定向到选定的类别页面。
提前谢谢你,亚历克斯
发布于 2017-04-20 12:29:20
解决我的问题的代码是:
<select name="cat" onChange="window.document.location.href=this.options[this.selectedIndex].value;"> 
                       <option value=""><?php echo esc_attr_e( 'Blog Categories', 'textdomain' ); ?></option> 
                    <?php 
                    $args = array(
                               'orderby' => 'ID',
                               'order'=> 'ASC',
                               'hide_empty' => 0,
                            );
                    $categories = get_categories($args); 
                    foreach ( $categories as $category ) {
                        $term_link = get_category_link($category->term_id );
                        $term_link = esc_url( $term_link );
                        echo '<option value="'.$term_link.'">'.$category->cat_name.'</option>';
                    }
                    ?>
                </select>发布于 2017-04-18 21:11:37
你可以试试这段代码...
<select name="event-dropdown"> 
    <option value=""><?php echo esc_attr_e( 'Blog Categories', 'textdomain' ); ?></option> 
    <?php 
    $args = array(
               'orderby' => 'ID',
               'order'=> 'ASC',
               'exclude' => array(1)
            );
    $categories = get_categories($args); 
    foreach ( $categories as $category ) {
        $term_link = get_category_link($category->term_id );
        $term_link = esc_url( $term_link );
        echo '<option value="'.$category->term_id.'"><a href="'.$term_link.'">'.$category->cat_name.'</a></option>';
    }
    ?>
</select>发布于 2017-04-19 21:26:06
谢谢你,纳温。看起来代码已经生成好了:
<select name="cat" id="cat-blog" class="form-control mt33"><option value="">Blog Categories</option> 
                    <option value="2"><a href="http://localhost/blog/category/cat1/">cat1</a></option><option value="3"><a href="http://localhost/blog/category/cat2/">cat2</a></option><option value="4"><a href="http://localhost/blog/category/cat3/">cat3</a></option><option value="5"><a href="http://localhost/blog/category/cat4">cat4</a></option><option value="6"><a href="http://localhost/blog/category/cat5">cat5</a></option></select>但问题是,当我选择其中一个选项时,链接不起作用。我不会被重定向到指定的类别页面。我有一个通用的category.php页面。
https://stackoverflow.com/questions/43472134
复制相似问题