Alex Blaga
Alex Blaga

Reputation: 41

Add a link to <option> elements generated by wp_dropdown_categories()

I'm very new at this. So what I've manage to do so far. I've used the following code to generate a element with all my first level categories:

 <?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 );
    ?>

But unfortunately I can't manage to add a <a href=""> to all the <option> tags generated by wp_dropdown_categories function.

Is it possible to add a link for every generated category. I want to redirect the users to selected category page.

Thanks in advance Alex

Upvotes: 1

Views: 2525

Answers (3)

Alex Blaga
Alex Blaga

Reputation: 41

The code that solved my problem is:

<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>

Upvotes: 3

Alex Blaga
Alex Blaga

Reputation: 41

Thank you Naveen. It seems that the code is generated ok:

<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>

But the problem is that, when I select one of the options, the link doesn't work. I'm not redirected to the indicated category page. I have a generic category.php page in place.

Upvotes: 0

Naveen Giri
Naveen Giri

Reputation: 545

You can try this code....

<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>

Upvotes: 0

Related Questions