class-walker-category-dropdown.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. * @var string
  22. *
  23. * @see Walker::$tree_type
  24. */
  25. public $tree_type = 'category';
  26. /**
  27. * Database fields to use.
  28. *
  29. * @since 2.1.0
  30. * @todo Decouple this
  31. * @var array
  32. *
  33. * @see Walker::$db_fields
  34. */
  35. public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
  36. /**
  37. * Starts the element output.
  38. *
  39. * @since 2.1.0
  40. *
  41. * @see Walker::start_el()
  42. *
  43. * @param string $output Used to append additional content (passed by reference).
  44. * @param object $category Category data object.
  45. * @param int $depth Depth of category. Used for padding.
  46. * @param array $args Uses 'selected', 'show_count', and 'value_field' keys, if they exist.
  47. * See wp_dropdown_categories().
  48. * @param int $id Optional. ID of the current category. Default 0 (unused).
  49. */
  50. public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
  51. $pad = str_repeat('&nbsp;', $depth * 3);
  52. /** This filter is documented in wp-includes/category-template.php */
  53. $cat_name = apply_filters( 'list_cats', $category->name, $category );
  54. if ( isset( $args['value_field'] ) && isset( $category->{$args['value_field']} ) ) {
  55. $value_field = $args['value_field'];
  56. } else {
  57. $value_field = 'term_id';
  58. }
  59. $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$value_field} ) . "\"";
  60. // Type-juggling causes false matches, so we force everything to a string.
  61. if ( (string) $category->{$value_field} === (string) $args['selected'] )
  62. $output .= ' selected="selected"';
  63. $output .= '>';
  64. $output .= $pad.$cat_name;
  65. if ( $args['show_count'] )
  66. $output .= '&nbsp;&nbsp;('. number_format_i18n( $category->count ) .')';
  67. $output .= "</option>\n";
  68. }
  69. }