RUSSEL86

Display Category list as select option with Link

Feb 5th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.15 KB | None | 0 0
  1. --------------------------------------
  2. paste this code into functions.php
  3. --------------------------------------
  4. function get_terms_dropdown($taxonomies, $args){
  5.     $myterms = get_terms($taxonomies, $args);
  6.     $output ="<select class='cat_select' onChange='window.location.href=this.value'>";
  7.     foreach($myterms as $term){
  8.         $root_url = get_bloginfo('url');
  9.         $term_taxonomy=$term->taxonomy;
  10.         $term_slug=$term->slug;
  11.         $term_name =$term->name;
  12.         $link = $root_url.'/'.$term_taxonomy.'/'.$term_slug;
  13.         $output .="<option value='".$link."'>".$term_name."</option>";
  14.     }
  15.     $output .="</select>";
  16. return $output;
  17. }
  18.  
  19. --------------------------------------------------------------------------
  20. Paste where you want display category list as select option with link
  21. --------------------------------------------------------------------------
  22. <?php $taxonomies = array('category');
  23. $args = array('orderby'=>'title','hide_empty'=>true);
  24. echo get_terms_dropdown($taxonomies, $args); ?>
  25. -------   or   -------------
  26. $taxonomies = array('category');
  27. $args = array('orderby'=>'count','hide_empty'=>true);
  28. echo get_terms_dropdown($taxonomies, $args);
  29.  
  30.  
  31. happy coding :)
Advertisement
Add Comment
Please, Sign In to add comment