You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

81 lines
2.1 KiB

  1. <?php
  2. /**
  3. * Taxonomy API: Walker_CategoryDropdown class
  4. *
  5. * @package WordPress
  6. * @subpackage Template
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to create an HTML dropdown list of Categories.
  11. *
  12. * @since 2.1.0
  13. *
  14. * @see Walker
  15. */
  16. class Walker_CategoryDropdown extends Walker {
  17. /**
  18. * What the class handles.
  19. *
  20. * @since 2.1.0
  21. * @access private
  22. * @var string
  23. *
  24. * @see Walker::$tree_type
  25. */
  26. public $tree_type = 'category';
  27. /**
  28. * Database fields to use.
  29. *
  30. * @since 2.1.0
  31. * @access public
  32. * @todo Decouple this
  33. * @var array
  34. *
  35. * @see Walker::$db_fields
  36. */
  37. public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
  38. /**
  39. * Starts the element output.
  40. *
  41. * @since 2.1.0
  42. * @access public
  43. *
  44. * @see Walker::start_el()
  45. *
  46. * @param string $output Passed by reference. Used to append additional content.
  47. * @param object $category Category data object.
  48. * @param int $depth Depth of category. Used for padding.
  49. * @param array $args Uses 'selected', 'show_count', and 'value_field' keys, if they exist.
  50. * See wp_dropdown_categories().
  51. * @param int $id Optional. ID of the current category. Default 0 (unused).
  52. */
  53. public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
  54. $pad = str_repeat('&nbsp;', $depth * 3);
  55. /** This filter is documented in wp-includes/category-template.php */
  56. $cat_name = apply_filters( 'list_cats', $category->name, $category );
  57. if ( isset( $args['value_field'] ) && isset( $category->{$args['value_field']} ) ) {
  58. $value_field = $args['value_field'];
  59. } else {
  60. $value_field = 'term_id';
  61. }
  62. $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$value_field} ) . "\"";
  63. // Type-juggling causes false matches, so we force everything to a string.
  64. if ( (string) $category->{$value_field} === (string) $args['selected'] )
  65. $output .= ' selected="selected"';
  66. $output .= '>';
  67. $output .= $pad.$cat_name;
  68. if ( $args['show_count'] )
  69. $output .= '&nbsp;&nbsp;('. number_format_i18n( $category->count ) .')';
  70. $output .= "</option>\n";
  71. }
  72. }