bradtrivers

ecpt_fbt_add_taxonomy_filters

Feb 17th, 2012
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.26 KB | None | 0 0
  1.     // registers each of the taxonomy filter drop downs
  2.     function ecpt_fbt_add_taxonomy_filters() {
  3.         global $typenow;            // the current post type
  4.         $taxonomies = get_taxonomies('','objects');
  5.         foreach($taxonomies as $taxName => $tax) {
  6.           if(in_array($typenow,$tax->object_type) && $taxName != 'category' && $taxName != 'tags') {
  7.             $terms = get_terms($taxName);
  8.             if(count($terms) > 0) {
  9.               //Check if hierarchical - if so build hierarchical drop-down
  10.               if($tax->hierarchical) {
  11.                 $args = array(
  12.                           'show_option_all'    => 'All '.$tax->labels->name,
  13.                           'show_option_none'   => 'Select '.$tax->labels->name,
  14.                           'show_count'         => 1,
  15.                           'hide_empty'         => 0,
  16.                           'echo'               => 1,
  17.                           'hierarchical'       => 1,
  18.                           'depth'              => 3,
  19.                           'name'               => $tax->rewrite['slug'],
  20.                           'id'                 => $tax->rewrite['slug'],                      
  21.                           'class'              => 'postform',
  22.                           'depth'              => 0,
  23.                           'tab_index'          => 0,
  24.                           'taxonomy'           => $taxName,
  25.                           'hide_if_empty'      => false);
  26.                       $args['walker'] = new Walker_FilterByTaxonomy;
  27.                     wp_dropdown_categories($args);
  28.              } else {
  29.                 echo "<select name='".$tax->rewrite['slug']."' id='".$tax->rewrite['slug']."' class='postform'>";
  30.                 echo "<option value=''>Show All ".$tax->labels->name."</option>";
  31.                 foreach ($terms as $term) {
  32.                       echo '<option value="' . $term->slug . '"', $_GET[$taxName] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
  33.                     }
  34.                 echo "</select>";
  35.             }
  36.               }
  37.           }
  38.         }
  39.     }
  40.     add_action( 'restrict_manage_posts', 'ecpt_fbt_add_taxonomy_filters', 100 );
  41.    
  42.     /**
  43.      * Create HTML dropdown list of Categories.
  44.      *
  45.      * @package WordPress
  46.      * @since 2.1.0
  47.      * @uses Walker
  48.      */
  49.     class Walker_FilterByTaxonomy extends Walker {
  50.         var $tree_type = 'category';
  51.         var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
  52.         function start_el(&$output, $category, $depth, $args) {
  53.           $args['selected'] = get_query_var( $args['taxonomy'] );
  54.             $pad = str_repeat('&nbsp;', $depth * 3);
  55.      
  56.             $cat_name = apply_filters('list_cats', $category->name, $category);
  57.             $output .= "\t<option class=\"level-$depth\" value=\"".$category->slug."\"";
  58.             if ( $category->slug == $args['selected'] )
  59.               $output .= ' selected="selected"';
  60.             $output .= '>';
  61.             $output .= $pad.$cat_name;
  62.             if ( $args['show_count'] )
  63.                 $output .= '&nbsp;&nbsp;('. $category->count .')';
  64.             if ( $args['show_last_update'] ) {
  65.                 $format = 'Y-m-d';
  66.                 $output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
  67.             }
  68.             $output .= "</option>\n";
  69.         }
  70.    
  71.     }
Advertisement
Add Comment
Please, Sign In to add comment