Subcategories on dropdown
-
Hello, this is a great plugin! I am wondering if it’s possible to differentiate parent and child categories on taxonomy dropdowns, and order them in a way that a subcategory would indent under its parent category, e.g:
Parent 1
– a
– b
– cParent 2
– a
– b
– cI am guessing I would have to add a filter to modify their order? but I am not sure I know the right args to achieve what I want, any thoughts will be appreciated!
https://www.ads-software.com/plugins/ultimate-wp-query-search-filter/
-
Yes, you will need to use the filter to customize it.
If you know coding and understand wp filter, you can see for the script here, it is the class that generate the field.Look for ‘output_formtaxo_fields’ function, it is where the field generated.
Hey TC.K, thanks for your quick reply, I am not very familiar but I will give it a try and post back with any solution I find for my problem. However I believe this should be default behavior for sub-categories as I don’t think anyone with multiple level categories expects to have them all on the same level.
Thanks again!
Ok so I’ve managed to solve my problem, I am sure there must be a much cleaner way to achieve this and I would appreciate if anyone could post it here.
In my theme functions file I have added the following filters:
//Display parent categories only add_filter('uwpqsf_taxonomy_arg', 'custom_term_output','',1); function custom_term_output($args){ $args['parent'] = '0'; return $args; } //MODIFY TAXFIELD DROPDOWN OUTPUT TO IDENTIFY AND STYLE CHILD CATEGORIES add_filter('uwpqsf_tax_field_dropdown','custom_dropdown_output','',12); function custom_dropdown_output($html,$type,$exc,$hide,$taxname,$taxlabel,$taxall,$opt,$c,$defaultclass,$formid,$divclass){ $args = array('hide_empty'=>$hide,'exclude'=>$eid ); $taxoargs = apply_filters('uwpqsf_taxonomy_arg',$args,$taxname,$formid); $terms = get_terms($taxname,$taxoargs); $count = count($terms); if($type == 'dropdown'){ $html = '<div class="'.$defaultclass.' '.$divclass.' tax-select-'.$c.'"><span class="taxolabel-'.$c.'">'.$taxlabel.'</span>'; $html .= '<input type="hidden" name="taxo['.$c.'][name]" value="'.$taxname.'">'; $html .= '<input type="hidden" name="taxo['.$c.'][opt]" value="'.$opt.'">'; $html .= '<select id="tdp-'.$c.'" name="taxo['.$c.'][term]">'; if(!empty($taxall)){ $html .= '<option selected value="uwpqsftaxoall">'.$taxall.'</option>'; } if ( $count > 0 ){ foreach ( $terms as $term ) { $selected = $terms[0]->term_id; $html .= '<option value="'.$term->slug.'">'.$term->name.'</option>'; $args = array( 'hide_empty' => false, 'hierarchical' => true, 'parent' => $term->term_id ); $childterms = get_terms($taxname, $args); foreach ( $childterms as $childterm ) { $selected = $childterms[0]->term_id; $html .= "<option value='".$childterm->slug."'"."> >" . $childterm->name . '</option>'; }} } $html .= '</select>'; $html .= '</div>'; return apply_filters( 'custom_dropdown_output', $html,$type,$exc,$hide,$taxname,$taxlabel,$taxall,$opt,$c,$defaultclass,$formid,$divclass); } }
With this code I managed to have my subcategories indented under their parent categories.
Thanks for pointing me in the right direction TC.K, I hope something like this can be added to the plugin so users can have this behavior by default.
Hey jaydeesigns, thats cool! Works perfectly with drop-down. I’m trying to do the same with radio button list, the problem is that children are repeated at the end of the listing. Can you help?
The code im using:
add_filter('uwpqsf_tax_field_radio','custom_radio_output','',12); function custom_radio_output($html,$type,$exc,$hide,$taxname,$taxlabel,$taxall,$opt,$c,$defaultclass,$formid,$divclass){ $args = array('hide_empty'=>$hide,'exclude'=>$eid ); $taxoargs = apply_filters('uwpqsf_taxonomy_arg',$args,$taxname,$formid); $terms = get_terms($taxname,$taxoargs); $count = count($terms); if($type == 'radio'){ $html = '<div class="'.$defaultclass.' '.$divclass.' tax-select-'.$c.'"><span class="taxolabel-'.$c.'">'.$taxlabel.'</span>'; $html .= '<input type="hidden" name="taxo['.$c.'][name]" value="'.$taxname.'">'; $html .= '<input type="hidden" name="taxo['.$c.'][opt]" value="'.$opt.'">'; if(!empty($taxall)){ $html .= '<label><input type="radio" id="tradio-'.$c.'" name="taxo['.$c.'][term]" selected value="uwpqsftaxoall">'.$taxall.'</label>'; } if ( $count > 0 ){ foreach ( $terms as $term ) { $selected = $terms[0]->term_id; $html .= '<label><input type="radio" id="tradio-'.$c.'" name="taxo['.$c.'][term]" value="'.$term->slug.'">'.$term->name.'</label>'; $args = array( 'hide_empty' => false, 'hierarchical' => true, 'parent' => $term->term_id ); $childterms = get_terms($taxname, $args); foreach ( $childterms as $childterm ) { $selected = $childterms[0]->term_id; $html .= "<label class='child'><input type='radio' id='tradio-".$c."' name='taxo[".$c."][term]' value='".$childterm->slug."'".">" . $childterm->name . '</label>'; }} } $html .= '</div>'; return apply_filters( 'custom_radio_output', $html,$type,$exc,$hide,$taxname,$taxlabel,$taxall,$opt,$c,$defaultclass,$formid,$divclass); } }
Hi jaydeesigns,
I’ve just tried your code above and what it does for me is hide all the child categories rather than show them indented (or whatever heirarchy style).
Can’t seem to get it working has anyone else had that occur?
Hey enekobarrero, I am sorry I hadn’t seen your message before. I hope you got it working. I haven’t used the radio button list so if you got it working, it’d be cool to see your solution.
@igloobob. Don’t know why your child categories wouldn’t show, check out the source code to confirm they are not being output.
Hi jaydeesigns, thanks fro replying. I figured out that it is because I have another function to show the post count. They won’t work together though, whichever I put last in the functions file works, but not both (so your code above does in fact work for me thanks!). I don’t know how to combine them to both work.
Don’t suppose you know?
// Ultimate WP Query Search Filter heirarchy in drop downs add_filter('uwpqsf_taxonomy_arg', 'custom_term_output','',1); function custom_term_output($args){ $args['parent'] = '0'; return $args; } //MODIFY TAXFIELD DROPDOWN OUTPUT TO IDENTIFY AND STYLE CHILD CATEGORIES add_filter('uwpqsf_tax_field_dropdown','custom_dropdown_output','',12); function custom_dropdown_output($html,$type,$exc,$hide,$taxname,$taxlabel,$taxall,$opt,$c,$defaultclass,$formid,$divclass){ $args = array('hide_empty'=>$hide,'exclude'=>$eid ); $taxoargs = apply_filters('uwpqsf_taxonomy_arg',$args,$taxname,$formid); $terms = get_terms($taxname,$taxoargs); $count = count($terms); if($type == 'dropdown'){ $html = '<div class="'.$defaultclass.' '.$divclass.' tax-select-'.$c.'"><span class="taxolabel-'.$c.'">'.$taxlabel.'</span>'; $html .= '<input type="hidden" name="taxo['.$c.'][name]" value="'.$taxname.'">'; $html .= '<input type="hidden" name="taxo['.$c.'][opt]" value="'.$opt.'">'; $html .= '<select id="tdp-'.$c.'" name="taxo['.$c.'][term]">'; if(!empty($taxall)){ $html .= '<option selected value="uwpqsftaxoall">'.$taxall.'</option>'; } if ( $count > 0 ){ foreach ( $terms as $term ) { $selected = $terms[0]->term_id; $html .= '<option value="'.$term->slug.'">'.$term->name.'</option>'; $args = array( 'hide_empty' => false, 'hierarchical' => true, 'parent' => $term->term_id ); $childterms = get_terms($taxname, $args); foreach ( $childterms as $childterm ) { $selected = $childterms[0]->term_id; $html .= "<option value='".$childterm->slug."'"."> >" . $childterm->name . '</option>'; }} } $html .= '</select>'; $html .= '</div>'; return apply_filters( 'custom_dropdown_output', $html,$type,$exc,$hide,$taxname,$taxlabel,$taxall,$opt,$c,$defaultclass,$formid,$divclass); } } // add post count to search drop downs - Ultimate WP Query Search Filter // https://9-sec.com/support-forum/?mingleforumaction=viewtopic&t=221 add_filter('uwpqsf_tax_field_dropdown','add_post_Cgg','',12); function add_post_Cgg($html ,$type,$exc,$hide,$taxname,$taxlabel,$taxall,$opt,$c,$defaultclass,$formid,$divclass){ $eid = explode(",", $exc); $args = array('hide_empty'=>$hide,'exclude'=>$eid ); $taxoargs = apply_filters('uwpqsf_taxonomy_arg',$args,$taxname,$formid); $terms = get_terms($taxname,$taxoargs); $count = count($terms); $html = ''; $html .= '<div class="'.$defaultclass.' '.$divclass.' tax-select-'.$c.'"><span class="taxolabel-'.$c.'">'.$taxlabel.'</span>'; $html .= '<input type="hidden" name="taxo['.$c.'][name]" value="'.$taxname.'">'; $html .= '<input type="hidden" name="taxo['.$c.'][opt]" value="'.$opt.'">'; $html .= '<select id="tdp-'.$c.'" name="taxo['.$c.'][term]">'; if(!empty($taxall)){ $html .= '<option selected value="uwpqsftaxoall">'.$taxall.'</option>'; } foreach ( $terms as $term ) { $term_obj = get_term( $term->term_id, $taxname ); $html .= '<option value="'.$term->slug.'">'.$term->name.' ('.$term_obj->count.')</option>';} $html .= '</select>'; $html .= '</div>';return $html; }
Hello,
That’s a realy very good post. But please let me know if it’s posible to show the 3 levels, like that:
Paretnt1:
Subcat1:
Subcat11
Paretnt2:
Subcat2:
Subcat2Many regards
- The topic ‘Subcategories on dropdown’ is closed to new replies.