How to create drop down for child categories of current taxonomy being viewed?

I need to know how can I get the child/grandchild terms of the current taxonomy being viewed, and put it into a drop down so that I can add values to the options.

I am currently using this code to display exactly what I want, but I am unable to modify the options in the drop down –

Read More
<?php

//first get the current term
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

//then set the args for wp_dropdown_categories
 $args = array(
    'child_of' => $current_term->term_id,
    'taxonomy' => $current_term->taxonomy,
    'hide_empty' => 0,
    'hierarchical' => true,
    'depth'  => 2,
    'title_li' => '',
        'show_option_all' => All,
        'hide_if_empty' => true
    );
 wp_dropdown_categories( $args );
?>

The other code Im using that I need it to look like or be similar to –

<?php
$terms = get_terms("videoscategory");
 $count = count($terms);
 if ( $count > 0 ){
     echo "<select id='filter-select2' style='width:225px;'>";
echo "<option value='*' data-filter-value='' class='selected'>All items</option>";
     foreach ( $terms as $term ) {
         echo "<option value='.{$term->slug}'>" . $term->name . "</option>";
     }
     echo "</select>";
 }
?>

So basically what I am looking for is a way to add ” value=’.{$term->slug}’ ” to the options of the first code which displays the child terms. Any idea how I can modify either one of the codes above so that I can add a the term name as a value to the drop down for child categories?

Related posts

Leave a Reply

1 comment

  1. Ok well without any help from here……. I was able to figure out how to change the option and select values of wp_dropdown_categories to display the child terms of the current taxonomy being viewed-
    First I placed this code into my functions file, which creates a walker class –

    class SH_Walker_TaxonomyDropdown extends Walker_CategoryDropdown{
    
        function start_el(&$output, $category, $depth, $args) {
            $pad = str_repeat('&nbsp;', $depth * 2);
            $cat_name = apply_filters('list_cats', $category->name, $category);
    
            if( !isset($args['value']) ){
                $args['value'] = ( $category->taxonomy != 'category' ? 'slug' : 'id' );
            }
    
            $value = ($args['value']=='slug' ? $category->slug : $category->term_id );
    
            $output .= "t<option class="level-$depth" data-filter-value=".".$value.""";
            if ( $value === (string) $args['selected'] ){ 
                $output .= ' selected="selected"';
            }
            $output .= '>';
            $output .= $pad.$cat_name;
            if ( $args['show_count'] )
                $output .= '&nbsp;&nbsp;('. $category->count .')';
    
            $output .= "</option>n";
    }
     }
    

    Here’s the code I placed in my sidebar –

    <?php
    
    //first get the current term
    $current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    
    //then set the args for wp_dropdown_categories
     $args = array(
        'walker'=> new SH_Walker_TaxonomyDropdown(),
        'value'=>'slug',
        'child_of' => $current_term->term_id,
        'taxonomy' => $current_term->taxonomy,
        'hide_empty' => 0,
        'hierarchical' => true,
        'depth'  => 2,
        'title_li' => '',
        'id' => 'filter-select',
        'class' => 'filter option-set',
            'show_option_all' => All,
            'hide_if_empty' => true
        );
     wp_dropdown_categories( $args );
    ?>