29 |
30 |
30 /** |
31 /** |
31 * Retrieve category parents with separator. |
32 * Retrieve category parents with separator. |
32 * |
33 * |
33 * @since 1.2.0 |
34 * @since 1.2.0 |
|
35 * @since 4.8.0 The `$visited` parameter was deprecated and renamed to `$deprecated`. |
34 * |
36 * |
35 * @param int $id Category ID. |
37 * @param int $id Category ID. |
36 * @param bool $link Optional, default is false. Whether to format with link. |
38 * @param bool $link Optional, default is false. Whether to format with link. |
37 * @param string $separator Optional, default is '/'. How to separate categories. |
39 * @param string $separator Optional, default is '/'. How to separate categories. |
38 * @param bool $nicename Optional, default is false. Whether to use nice name for display. |
40 * @param bool $nicename Optional, default is false. Whether to use nice name for display. |
39 * @param array $visited Optional. Already linked to categories to prevent duplicates. |
41 * @param array $deprecated Not used. |
40 * @return string|WP_Error A list of category parents on success, WP_Error on failure. |
42 * @return string|WP_Error A list of category parents on success, WP_Error on failure. |
41 */ |
43 */ |
42 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) { |
44 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $deprecated = array() ) { |
43 $chain = ''; |
45 |
44 $parent = get_term( $id, 'category' ); |
46 if ( ! empty( $deprecated ) ) { |
45 if ( is_wp_error( $parent ) ) |
47 _deprecated_argument( __FUNCTION__, '4.8.0' ); |
46 return $parent; |
48 } |
47 |
49 |
48 if ( $nicename ) |
50 $format = $nicename ? 'slug' : 'name'; |
49 $name = $parent->slug; |
51 |
50 else |
52 $args = array( |
51 $name = $parent->name; |
53 'separator' => $separator, |
52 |
54 'link' => $link, |
53 if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) { |
55 'format' => $format, |
54 $visited[] = $parent->parent; |
56 ); |
55 $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited ); |
57 |
56 } |
58 return get_term_parents_list( $id, 'category', $args ); |
57 |
|
58 if ( $link ) |
|
59 $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">'.$name.'</a>' . $separator; |
|
60 else |
|
61 $chain .= $name.$separator; |
|
62 return $chain; |
|
63 } |
59 } |
64 |
60 |
65 /** |
61 /** |
66 * Retrieve post categories. |
62 * Retrieve post categories. |
67 * |
63 * |
|
64 * This tag may be used outside The Loop by passing a post id as the parameter. |
|
65 * |
|
66 * Note: This function only returns results from the default "category" taxonomy. |
|
67 * For custom taxonomies use get_the_terms(). |
|
68 * |
68 * @since 0.71 |
69 * @since 0.71 |
69 * |
70 * |
70 * @param int $id Optional, default to current post ID. The post ID. |
71 * @param int $id Optional, default to current post ID. The post ID. |
71 * @return array |
72 * @return array Array of WP_Term objects, one for each category assigned to the post. |
72 */ |
73 */ |
73 function get_the_category( $id = false ) { |
74 function get_the_category( $id = false ) { |
74 $categories = get_the_terms( $id, 'category' ); |
75 $categories = get_the_terms( $id, 'category' ); |
75 if ( ! $categories || is_wp_error( $categories ) ) |
76 if ( ! $categories || is_wp_error( $categories ) ) |
76 $categories = array(); |
77 $categories = array(); |
138 * @param int $cat_ID Category ID. |
102 * @param int $cat_ID Category ID. |
139 * @return string|WP_Error Category name on success, WP_Error on failure. |
103 * @return string|WP_Error Category name on success, WP_Error on failure. |
140 */ |
104 */ |
141 function get_the_category_by_ID( $cat_ID ) { |
105 function get_the_category_by_ID( $cat_ID ) { |
142 $cat_ID = (int) $cat_ID; |
106 $cat_ID = (int) $cat_ID; |
143 $category = get_term( $cat_ID, 'category' ); |
107 $category = get_term( $cat_ID ); |
144 |
108 |
145 if ( is_wp_error( $category ) ) |
109 if ( is_wp_error( $category ) ) |
146 return $category; |
110 return $category; |
147 |
111 |
148 return ( $category ) ? $category->name : ''; |
112 return ( $category ) ? $category->name : ''; |
149 } |
113 } |
150 |
114 |
151 /** |
115 /** |
152 * Retrieve category list in either HTML list or custom format. |
116 * Retrieve category list for a post in either HTML list or custom format. |
153 * |
117 * |
154 * @since 1.5.1 |
118 * @since 1.5.1 |
155 * |
119 * |
156 * @param string $separator Optional, default is empty string. Separator for between the categories. |
120 * @global WP_Rewrite $wp_rewrite |
|
121 * |
|
122 * @param string $separator Optional. Separator between the categories. By default, the links are placed |
|
123 * in an unordered list. An empty string will result in the default behavior. |
157 * @param string $parents Optional. How to display the parents. |
124 * @param string $parents Optional. How to display the parents. |
158 * @param int $post_id Optional. Post ID to retrieve categories. |
125 * @param int $post_id Optional. Post ID to retrieve categories. |
159 * @return string |
126 * @return string |
160 */ |
127 */ |
161 function get_the_category_list( $separator = '', $parents='', $post_id = false ) { |
128 function get_the_category_list( $separator = '', $parents = '', $post_id = false ) { |
162 global $wp_rewrite; |
129 global $wp_rewrite; |
163 if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) ) { |
130 if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) ) { |
164 /** This filter is documented in wp-includes/category-template.php */ |
131 /** This filter is documented in wp-includes/category-template.php */ |
165 return apply_filters( 'the_category', '', $separator, $parents ); |
132 return apply_filters( 'the_category', '', $separator, $parents ); |
166 } |
133 } |
167 |
134 |
168 $categories = get_the_category( $post_id ); |
135 /** |
|
136 * Filters the categories before building the category list. |
|
137 * |
|
138 * @since 4.4.0 |
|
139 * |
|
140 * @param array $categories An array of the post's categories. |
|
141 * @param int|bool $post_id ID of the post we're retrieving categories for. When `false`, we assume the |
|
142 * current post in the loop. |
|
143 */ |
|
144 $categories = apply_filters( 'the_category_list', get_the_category( $post_id ), $post_id ); |
|
145 |
169 if ( empty( $categories ) ) { |
146 if ( empty( $categories ) ) { |
170 /** This filter is documented in wp-includes/category-template.php */ |
147 /** This filter is documented in wp-includes/category-template.php */ |
171 return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents ); |
148 return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents ); |
172 } |
149 } |
173 |
150 |
292 * display all of the categories. When it is enabled it will use the value in |
270 * display all of the categories. When it is enabled it will use the value in |
293 * the 'depth' argument. |
271 * the 'depth' argument. |
294 * |
272 * |
295 * @since 2.1.0 |
273 * @since 2.1.0 |
296 * @since 4.2.0 Introduced the `value_field` argument. |
274 * @since 4.2.0 Introduced the `value_field` argument. |
|
275 * @since 4.6.0 Introduced the `required` argument. |
297 * |
276 * |
298 * @param string|array $args { |
277 * @param string|array $args { |
299 * Optional. Array or string of arguments to generate a categories drop-down element. |
278 * Optional. Array or string of arguments to generate a categories drop-down element. See WP_Term_Query::__construct() |
|
279 * for information on additional accepted arguments. |
300 * |
280 * |
301 * @type string $show_option_all Text to display for showing all categories. Default empty. |
281 * @type string $show_option_all Text to display for showing all categories. Default empty. |
302 * @type string $show_option_none Text to display for showing no categories. Default empty. |
282 * @type string $show_option_none Text to display for showing no categories. Default empty. |
303 * @type string $option_none_value Value to use when no category is selected. Default empty. |
283 * @type string $option_none_value Value to use when no category is selected. Default empty. |
304 * @type string $orderby Which column to use for ordering categories. See get_terms() for a list |
284 * @type string $orderby Which column to use for ordering categories. See get_terms() for a list |
305 * of accepted values. Default 'id' (term_id). |
285 * of accepted values. Default 'id' (term_id). |
306 * @type string $order Whether to order terms in ascending or descending order. Accepts 'ASC' |
|
307 * or 'DESC'. Default 'ASC'. |
|
308 * @type bool $pad_counts See get_terms() for an argument description. Default false. |
286 * @type bool $pad_counts See get_terms() for an argument description. Default false. |
309 * @type bool|int $show_count Whether to include post counts. Accepts 0, 1, or their bool equivalents. |
287 * @type bool|int $show_count Whether to include post counts. Accepts 0, 1, or their bool equivalents. |
310 * Default 0. |
288 * Default 0. |
311 * @type bool|int $hide_empty Whether to hide categories that don't have any posts. Accepts 0, 1, or |
|
312 * their bool equivalents. Default 1. |
|
313 * @type int $child_of Term ID to retrieve child terms of. See get_terms(). Default 0. |
|
314 * @type array|string $exclude Array or comma/space-separated string of term ids to exclude. |
|
315 * If `$include` is non-empty, `$exclude` is ignored. Default empty array. |
|
316 * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, or their |
289 * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, or their |
317 * bool equivalents. Default 1. |
290 * bool equivalents. Default 1. |
318 * @type bool|int $hierarchical Whether to traverse the taxonomy hierarchy. Accepts 0, 1, or their bool |
291 * @type bool|int $hierarchical Whether to traverse the taxonomy hierarchy. Accepts 0, 1, or their bool |
319 * equivalents. Default 0. |
292 * equivalents. Default 0. |
320 * @type int $depth Maximum depth. Default 0. |
293 * @type int $depth Maximum depth. Default 0. |
326 * @type int|string $selected Value of the option that should be selected. Default 0. |
299 * @type int|string $selected Value of the option that should be selected. Default 0. |
327 * @type string $value_field Term field that should be used to populate the 'value' attribute |
300 * @type string $value_field Term field that should be used to populate the 'value' attribute |
328 * of the option elements. Accepts any valid term field: 'term_id', 'name', |
301 * of the option elements. Accepts any valid term field: 'term_id', 'name', |
329 * 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', |
302 * 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', |
330 * 'parent', 'count'. Default 'term_id'. |
303 * 'parent', 'count'. Default 'term_id'. |
331 * @type string $taxonomy Name of the category to retrieve. Default 'category'. |
304 * @type string|array $taxonomy Name of the category or categories to retrieve. Default 'category'. |
332 * @type bool $hide_if_empty True to skip generating markup if no categories are found. |
305 * @type bool $hide_if_empty True to skip generating markup if no categories are found. |
333 * Default false (create select element even if no categories are found). |
306 * Default false (create select element even if no categories are found). |
|
307 * @type bool $required Whether the `<select>` element should have the HTML5 'required' attribute. |
|
308 * Default false. |
334 * } |
309 * } |
335 * @return string HTML content only if 'echo' argument is 0. |
310 * @return string HTML content only if 'echo' argument is 0. |
336 */ |
311 */ |
337 function wp_dropdown_categories( $args = '' ) { |
312 function wp_dropdown_categories( $args = '' ) { |
338 $defaults = array( |
313 $defaults = array( |
339 'show_option_all' => '', 'show_option_none' => '', |
314 'show_option_all' => '', |
340 'orderby' => 'id', 'order' => 'ASC', |
315 'show_option_none' => '', |
341 'show_count' => 0, |
316 'orderby' => 'id', |
342 'hide_empty' => 1, 'child_of' => 0, |
317 'order' => 'ASC', |
343 'exclude' => '', 'echo' => 1, |
318 'show_count' => 0, |
344 'selected' => 0, 'hierarchical' => 0, |
319 'hide_empty' => 1, |
345 'name' => 'cat', 'id' => '', |
320 'child_of' => 0, |
346 'class' => 'postform', 'depth' => 0, |
321 'exclude' => '', |
347 'tab_index' => 0, 'taxonomy' => 'category', |
322 'echo' => 1, |
348 'hide_if_empty' => false, 'option_none_value' => -1, |
323 'selected' => 0, |
349 'value_field' => 'term_id', |
324 'hierarchical' => 0, |
|
325 'name' => 'cat', |
|
326 'id' => '', |
|
327 'class' => 'postform', |
|
328 'depth' => 0, |
|
329 'tab_index' => 0, |
|
330 'taxonomy' => 'category', |
|
331 'hide_if_empty' => false, |
|
332 'option_none_value' => -1, |
|
333 'value_field' => 'term_id', |
|
334 'required' => false, |
350 ); |
335 ); |
351 |
336 |
352 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0; |
337 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0; |
353 |
338 |
354 // Back compat. |
339 // Back compat. |
355 if ( isset( $args['type'] ) && 'link' == $args['type'] ) { |
340 if ( isset( $args['type'] ) && 'link' == $args['type'] ) { |
356 _deprecated_argument( __FUNCTION__, '3.0', '' ); |
341 _deprecated_argument( __FUNCTION__, '3.0.0', |
|
342 /* translators: 1: "type => link", 2: "taxonomy => link_category" */ |
|
343 sprintf( __( '%1$s is deprecated. Use %2$s instead.' ), |
|
344 '<code>type => link</code>', |
|
345 '<code>taxonomy => link_category</code>' |
|
346 ) |
|
347 ); |
357 $args['taxonomy'] = 'link_category'; |
348 $args['taxonomy'] = 'link_category'; |
358 } |
349 } |
359 |
350 |
360 $r = wp_parse_args( $args, $defaults ); |
351 $r = wp_parse_args( $args, $defaults ); |
361 $option_none_value = $r['option_none_value']; |
352 $option_none_value = $r['option_none_value']; |
377 $categories = get_terms( $r['taxonomy'], $get_terms_args ); |
368 $categories = get_terms( $r['taxonomy'], $get_terms_args ); |
378 |
369 |
379 $name = esc_attr( $r['name'] ); |
370 $name = esc_attr( $r['name'] ); |
380 $class = esc_attr( $r['class'] ); |
371 $class = esc_attr( $r['class'] ); |
381 $id = $r['id'] ? esc_attr( $r['id'] ) : $name; |
372 $id = $r['id'] ? esc_attr( $r['id'] ) : $name; |
|
373 $required = $r['required'] ? 'required' : ''; |
382 |
374 |
383 if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) { |
375 if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) { |
384 $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n"; |
376 $output = "<select $required name='$name' id='$id' class='$class' $tab_index_attribute>\n"; |
385 } else { |
377 } else { |
386 $output = ''; |
378 $output = ''; |
387 } |
379 } |
388 if ( empty( $categories ) && ! $r['hide_if_empty'] && ! empty( $r['show_option_none'] ) ) { |
380 if ( empty( $categories ) && ! $r['hide_if_empty'] && ! empty( $r['show_option_none'] ) ) { |
389 |
381 |
390 /** |
382 /** |
391 * Filter a taxonomy drop-down display element. |
383 * Filters a taxonomy drop-down display element. |
392 * |
384 * |
393 * A variety of taxonomy drop-down display elements can be modified |
385 * A variety of taxonomy drop-down display elements can be modified |
394 * just prior to display via this filter. Filterable arguments include |
386 * just prior to display via this filter. Filterable arguments include |
395 * 'show_option_none', 'show_option_all', and various forms of the |
387 * 'show_option_none', 'show_option_all', and various forms of the |
396 * term name. |
388 * term name. |
397 * |
389 * |
398 * @since 1.2.0 |
390 * @since 1.2.0 |
399 * |
391 * |
400 * @see wp_dropdown_categories() |
392 * @see wp_dropdown_categories() |
401 * |
393 * |
402 * @param string $element Taxonomy element to list. |
394 * @param string $element Category name. |
|
395 * @param WP_Term|null $category The category object, or null if there's no corresponding category. |
403 */ |
396 */ |
404 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'] ); |
397 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'], null ); |
405 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "' selected='selected'>$show_option_none</option>\n"; |
398 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "' selected='selected'>$show_option_none</option>\n"; |
406 } |
399 } |
407 |
400 |
408 if ( ! empty( $categories ) ) { |
401 if ( ! empty( $categories ) ) { |
409 |
402 |
410 if ( $r['show_option_all'] ) { |
403 if ( $r['show_option_all'] ) { |
411 |
404 |
412 /** This filter is documented in wp-includes/category-template.php */ |
405 /** This filter is documented in wp-includes/category-template.php */ |
413 $show_option_all = apply_filters( 'list_cats', $r['show_option_all'] ); |
406 $show_option_all = apply_filters( 'list_cats', $r['show_option_all'], null ); |
414 $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : ''; |
407 $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : ''; |
415 $output .= "\t<option value='0'$selected>$show_option_all</option>\n"; |
408 $output .= "\t<option value='0'$selected>$show_option_all</option>\n"; |
416 } |
409 } |
417 |
410 |
418 if ( $r['show_option_none'] ) { |
411 if ( $r['show_option_none'] ) { |
419 |
412 |
420 /** This filter is documented in wp-includes/category-template.php */ |
413 /** This filter is documented in wp-includes/category-template.php */ |
421 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'] ); |
414 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'], null ); |
422 $selected = selected( $option_none_value, $r['selected'], false ); |
415 $selected = selected( $option_none_value, $r['selected'], false ); |
423 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$selected>$show_option_none</option>\n"; |
416 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$selected>$show_option_none</option>\n"; |
424 } |
417 } |
425 |
418 |
426 if ( $r['hierarchical'] ) { |
419 if ( $r['hierarchical'] ) { |
451 } |
444 } |
452 |
445 |
453 /** |
446 /** |
454 * Display or retrieve the HTML list of categories. |
447 * Display or retrieve the HTML list of categories. |
455 * |
448 * |
456 * The list of arguments is below: |
|
457 * 'show_option_all' (string) - Text to display for showing all categories. |
|
458 * 'orderby' (string) default is 'ID' - What column to use for ordering the |
|
459 * categories. |
|
460 * 'order' (string) default is 'ASC' - What direction to order categories. |
|
461 * 'show_count' (bool|int) default is 0 - Whether to show how many posts are |
|
462 * in the category. |
|
463 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that |
|
464 * don't have any posts attached to them. |
|
465 * 'use_desc_for_title' (bool|int) default is 1 - Whether to use the |
|
466 * category description as the title attribute. |
|
467 * 'feed' - See {@link get_categories()}. |
|
468 * 'feed_type' - See {@link get_categories()}. |
|
469 * 'feed_image' - See {@link get_categories()}. |
|
470 * 'child_of' (int) default is 0 - See {@link get_categories()}. |
|
471 * 'exclude' (string) - See {@link get_categories()}. |
|
472 * 'exclude_tree' (string) - See {@link get_categories()}. |
|
473 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content. |
|
474 * 'current_category' (int) - See {@link get_categories()}. |
|
475 * 'hierarchical' (bool) - See {@link get_categories()}. |
|
476 * 'title_li' (string) - See {@link get_categories()}. |
|
477 * 'depth' (int) - The max depth. |
|
478 * |
|
479 * @since 2.1.0 |
449 * @since 2.1.0 |
480 * |
450 * @since 4.4.0 Introduced the `hide_title_if_empty` and `separator` arguments. The `current_category` argument was modified to |
481 * @param string|array $args Optional. Override default arguments. |
451 * optionally accept an array of values. |
482 * @return false|null|string HTML content only if 'echo' argument is 0. |
452 * |
|
453 * @param string|array $args { |
|
454 * Array of optional arguments. |
|
455 * |
|
456 * @type int $child_of Term ID to retrieve child terms of. See get_terms(). Default 0. |
|
457 * @type int|array $current_category ID of category, or array of IDs of categories, that should get the |
|
458 * 'current-cat' class. Default 0. |
|
459 * @type int $depth Category depth. Used for tab indentation. Default 0. |
|
460 * @type bool|int $echo True to echo markup, false to return it. Default 1. |
|
461 * @type array|string $exclude Array or comma/space-separated string of term IDs to exclude. |
|
462 * If `$hierarchical` is true, descendants of `$exclude` terms will also |
|
463 * be excluded; see `$exclude_tree`. See get_terms(). |
|
464 * Default empty string. |
|
465 * @type array|string $exclude_tree Array or comma/space-separated string of term IDs to exclude, along |
|
466 * with their descendants. See get_terms(). Default empty string. |
|
467 * @type string $feed Text to use for the feed link. Default 'Feed for all posts filed |
|
468 * under [cat name]'. |
|
469 * @type string $feed_image URL of an image to use for the feed link. Default empty string. |
|
470 * @type string $feed_type Feed type. Used to build feed link. See get_term_feed_link(). |
|
471 * Default empty string (default feed). |
|
472 * @type bool|int $hide_empty Whether to hide categories that don't have any posts attached to them. |
|
473 * Default 1. |
|
474 * @type bool $hide_title_if_empty Whether to hide the `$title_li` element if there are no terms in |
|
475 * the list. Default false (title will always be shown). |
|
476 * @type bool $hierarchical Whether to include terms that have non-empty descendants. |
|
477 * See get_terms(). Default true. |
|
478 * @type string $order Which direction to order categories. Accepts 'ASC' or 'DESC'. |
|
479 * Default 'ASC'. |
|
480 * @type string $orderby The column to use for ordering categories. Default 'name'. |
|
481 * @type string $separator Separator between links. Default '<br />'. |
|
482 * @type bool|int $show_count Whether to show how many posts are in the category. Default 0. |
|
483 * @type string $show_option_all Text to display for showing all categories. Default empty string. |
|
484 * @type string $show_option_none Text to display for the 'no categories' option. |
|
485 * Default 'No categories'. |
|
486 * @type string $style The style used to display the categories list. If 'list', categories |
|
487 * will be output as an unordered list. If left empty or another value, |
|
488 * categories will be output separated by `<br>` tags. Default 'list'. |
|
489 * @type string $taxonomy Taxonomy name. Default 'category'. |
|
490 * @type string $title_li Text to use for the list title `<li>` element. Pass an empty string |
|
491 * to disable. Default 'Categories'. |
|
492 * @type bool|int $use_desc_for_title Whether to use the category description as the title attribute. |
|
493 * Default 1. |
|
494 * } |
|
495 * @return false|string HTML content only if 'echo' argument is 0. |
483 */ |
496 */ |
484 function wp_list_categories( $args = '' ) { |
497 function wp_list_categories( $args = '' ) { |
485 $defaults = array( |
498 $defaults = array( |
486 'show_option_all' => '', 'show_option_none' => __('No categories'), |
499 'child_of' => 0, |
487 'orderby' => 'name', 'order' => 'ASC', |
500 'current_category' => 0, |
488 'style' => 'list', |
501 'depth' => 0, |
489 'show_count' => 0, 'hide_empty' => 1, |
502 'echo' => 1, |
490 'use_desc_for_title' => 1, 'child_of' => 0, |
503 'exclude' => '', |
491 'feed' => '', 'feed_type' => '', |
504 'exclude_tree' => '', |
492 'feed_image' => '', 'exclude' => '', |
505 'feed' => '', |
493 'exclude_tree' => '', 'current_category' => 0, |
506 'feed_image' => '', |
494 'hierarchical' => true, 'title_li' => __( 'Categories' ), |
507 'feed_type' => '', |
495 'echo' => 1, 'depth' => 0, |
508 'hide_empty' => 1, |
496 'taxonomy' => 'category' |
509 'hide_title_if_empty' => false, |
|
510 'hierarchical' => true, |
|
511 'order' => 'ASC', |
|
512 'orderby' => 'name', |
|
513 'separator' => '<br />', |
|
514 'show_count' => 0, |
|
515 'show_option_all' => '', |
|
516 'show_option_none' => __( 'No categories' ), |
|
517 'style' => 'list', |
|
518 'taxonomy' => 'category', |
|
519 'title_li' => __( 'Categories' ), |
|
520 'use_desc_for_title' => 1, |
497 ); |
521 ); |
498 |
522 |
499 $r = wp_parse_args( $args, $defaults ); |
523 $r = wp_parse_args( $args, $defaults ); |
500 |
524 |
501 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) |
525 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) |
502 $r['pad_counts'] = true; |
526 $r['pad_counts'] = true; |
503 |
527 |
|
528 // Descendants of exclusions should be excluded too. |
504 if ( true == $r['hierarchical'] ) { |
529 if ( true == $r['hierarchical'] ) { |
505 $r['exclude_tree'] = $r['exclude']; |
530 $exclude_tree = array(); |
|
531 |
|
532 if ( $r['exclude_tree'] ) { |
|
533 $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $r['exclude_tree'] ) ); |
|
534 } |
|
535 |
|
536 if ( $r['exclude'] ) { |
|
537 $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $r['exclude'] ) ); |
|
538 } |
|
539 |
|
540 $r['exclude_tree'] = $exclude_tree; |
506 $r['exclude'] = ''; |
541 $r['exclude'] = ''; |
507 } |
542 } |
508 |
543 |
509 if ( ! isset( $r['class'] ) ) |
544 if ( ! isset( $r['class'] ) ) |
510 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy']; |
545 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy']; |
591 * |
652 * |
592 * The 'number' argument is how many tags to return. By default, the limit will |
653 * The 'number' argument is how many tags to return. By default, the limit will |
593 * be to return the top 45 tags in the tag cloud list. |
654 * be to return the top 45 tags in the tag cloud list. |
594 * |
655 * |
595 * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the |
656 * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the |
596 * text for the tooltip of the tag link. |
657 * text for the tag link count. |
597 * |
658 * |
598 * The 'topic_count_text_callback' argument is a function, which given the count |
659 * The 'topic_count_text_callback' argument is a function, which given the count |
599 * of the posts with that tag returns a text for the tooltip of the tag link. |
660 * of the posts with that tag returns a text for the tag link count. |
600 * |
661 * |
601 * The 'post_type' argument is used only when 'link' is set to 'edit'. It determines the post_type |
662 * The 'post_type' argument is used only when 'link' is set to 'edit'. It determines the post_type |
602 * passed to edit.php for the popular tags edit links. |
663 * passed to edit.php for the popular tags edit links. |
603 * |
664 * |
604 * The 'exclude' and 'include' arguments are used for the {@link get_tags()} |
665 * The 'exclude' and 'include' arguments are used for the get_tags() function. Only one |
605 * function. Only one should be used, because only one will be used and the |
666 * should be used, because only one will be used and the other ignored, if they are both set. |
606 * other ignored, if they are both set. |
|
607 * |
667 * |
608 * @since 2.3.0 |
668 * @since 2.3.0 |
|
669 * @since 4.8.0 Added the `show_count` argument. |
609 * |
670 * |
610 * @param array|string|null $args Optional. Override default arguments. |
671 * @param array|string|null $args Optional. Override default arguments. |
611 * @return null|false Generated tag cloud, only if no failures and 'array' is set for the 'format' argument. |
672 * @return void|array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument. |
|
673 * Otherwise, this function outputs the tag cloud. |
612 */ |
674 */ |
613 function wp_tag_cloud( $args = '' ) { |
675 function wp_tag_cloud( $args = '' ) { |
614 $defaults = array( |
676 $defaults = array( |
615 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, |
677 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, |
616 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', |
678 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', |
617 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true |
679 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true, |
|
680 'show_count' => 0, |
618 ); |
681 ); |
619 $args = wp_parse_args( $args, $defaults ); |
682 $args = wp_parse_args( $args, $defaults ); |
620 |
683 |
621 $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags |
684 $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags |
622 |
685 |
652 |
715 |
653 echo $return; |
716 echo $return; |
654 } |
717 } |
655 |
718 |
656 /** |
719 /** |
657 * Default topic count scaling for tag links |
720 * Default topic count scaling for tag links. |
658 * |
721 * |
659 * @param integer $count number of posts with that tag |
722 * @since 2.9.0 |
660 * @return integer scaled count |
723 * |
|
724 * @param int $count Number of posts with that tag. |
|
725 * @return int Scaled count. |
661 */ |
726 */ |
662 function default_topic_count_scale( $count ) { |
727 function default_topic_count_scale( $count ) { |
663 return round(log10($count + 1) * 100); |
728 return round(log10($count + 1) * 100); |
664 } |
729 } |
665 |
730 |
666 /** |
731 /** |
667 * Generates a tag cloud (heatmap) from provided data. |
732 * Generates a tag cloud (heatmap) from provided data. |
668 * |
733 * |
669 * The text size is set by the 'smallest' and 'largest' arguments, which will |
|
670 * use the 'unit' argument value for the CSS text size unit. The 'format' |
|
671 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the |
|
672 * 'format' argument will separate tags with spaces. The list value for the |
|
673 * 'format' argument will format the tags in a UL HTML list. The array value for |
|
674 * the 'format' argument will return in PHP array type format. |
|
675 * |
|
676 * The 'tag_cloud_sort' filter allows you to override the sorting. |
|
677 * Passed to the filter: $tags array and $args array, has to return the $tags array |
|
678 * after sorting it. |
|
679 * |
|
680 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'. |
|
681 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or |
|
682 * 'RAND'. |
|
683 * |
|
684 * The 'number' argument is how many tags to return. By default, the limit will |
|
685 * be to return the entire tag cloud list. |
|
686 * |
|
687 * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the |
|
688 * text for the tooltip of the tag link. |
|
689 * |
|
690 * The 'topic_count_text_callback' argument is a function, which given the count |
|
691 * of the posts with that tag returns a text for the tooltip of the tag link. |
|
692 * |
|
693 * @todo Complete functionality. |
734 * @todo Complete functionality. |
694 * @since 2.3.0 |
735 * @since 2.3.0 |
|
736 * @since 4.8.0 Added the `show_count` argument. |
695 * |
737 * |
696 * @param array $tags List of tags. |
738 * @param array $tags List of tags. |
697 * @param string|array $args Optional, override default arguments. |
739 * @param string|array $args { |
|
740 * Optional. Array of string of arguments for generating a tag cloud. |
|
741 * |
|
742 * @type int $smallest Smallest font size used to display tags. Paired |
|
743 * with the value of `$unit`, to determine CSS text |
|
744 * size unit. Default 8 (pt). |
|
745 * @type int $largest Largest font size used to display tags. Paired |
|
746 * with the value of `$unit`, to determine CSS text |
|
747 * size unit. Default 22 (pt). |
|
748 * @type string $unit CSS text size unit to use with the `$smallest` |
|
749 * and `$largest` values. Accepts any valid CSS text |
|
750 * size unit. Default 'pt'. |
|
751 * @type int $number The number of tags to return. Accepts any |
|
752 * positive integer or zero to return all. |
|
753 * Default 0. |
|
754 * @type string $format Format to display the tag cloud in. Accepts 'flat' |
|
755 * (tags separated with spaces), 'list' (tags displayed |
|
756 * in an unordered list), or 'array' (returns an array). |
|
757 * Default 'flat'. |
|
758 * @type string $separator HTML or text to separate the tags. Default "\n" (newline). |
|
759 * @type string $orderby Value to order tags by. Accepts 'name' or 'count'. |
|
760 * Default 'name'. The {@see 'tag_cloud_sort'} filter |
|
761 * can also affect how tags are sorted. |
|
762 * @type string $order How to order the tags. Accepts 'ASC' (ascending), |
|
763 * 'DESC' (descending), or 'RAND' (random). Default 'ASC'. |
|
764 * @type int|bool $filter Whether to enable filtering of the final output |
|
765 * via {@see 'wp_generate_tag_cloud'}. Default 1|true. |
|
766 * @type string $topic_count_text Nooped plural text from _n_noop() to supply to |
|
767 * tag counts. Default null. |
|
768 * @type callable $topic_count_text_callback Callback used to generate nooped plural text for |
|
769 * tag counts based on the count. Default null. |
|
770 * @type callable $topic_count_scale_callback Callback used to determine the tag count scaling |
|
771 * value. Default default_topic_count_scale(). |
|
772 * @type bool|int $show_count Whether to display the tag counts. Default 0. Accepts |
|
773 * 0, 1, or their bool equivalents. |
|
774 * } |
698 * @return string|array Tag cloud as a string or an array, depending on 'format' argument. |
775 * @return string|array Tag cloud as a string or an array, depending on 'format' argument. |
699 */ |
776 */ |
700 function wp_generate_tag_cloud( $tags, $args = '' ) { |
777 function wp_generate_tag_cloud( $tags, $args = '' ) { |
701 $defaults = array( |
778 $defaults = array( |
702 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0, |
779 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0, |
703 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', |
780 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', |
704 'topic_count_text' => null, 'topic_count_text_callback' => null, |
781 'topic_count_text' => null, 'topic_count_text_callback' => null, |
705 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1, |
782 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1, |
|
783 'show_count' => 0, |
706 ); |
784 ); |
707 |
785 |
708 $args = wp_parse_args( $args, $defaults ); |
786 $args = wp_parse_args( $args, $defaults ); |
709 |
787 |
710 $return = ( 'array' === $args['format'] ) ? array() : ''; |
788 $return = ( 'array' === $args['format'] ) ? array() : ''; |
711 |
789 |
712 if ( empty( $tags ) ) { |
790 if ( empty( $tags ) ) { |
713 return $return; |
791 return $return; |
714 } |
792 } |
715 |
793 |
716 // Juggle topic count tooltips: |
794 // Juggle topic counts. |
717 if ( isset( $args['topic_count_text'] ) ) { |
795 if ( isset( $args['topic_count_text'] ) ) { |
718 // First look for nooped plural support via topic_count_text. |
796 // First look for nooped plural support via topic_count_text. |
719 $translate_nooped_plural = $args['topic_count_text']; |
797 $translate_nooped_plural = $args['topic_count_text']; |
720 } elseif ( ! empty( $args['topic_count_text_callback'] ) ) { |
798 } elseif ( ! empty( $args['topic_count_text_callback'] ) ) { |
721 // Look for the alternative callback style. Ignore the previous default. |
799 // Look for the alternative callback style. Ignore the previous default. |
722 if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) { |
800 if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) { |
723 $translate_nooped_plural = _n_noop( '%s topic', '%s topics' ); |
801 $translate_nooped_plural = _n_noop( '%s item', '%s items' ); |
724 } else { |
802 } else { |
725 $translate_nooped_plural = false; |
803 $translate_nooped_plural = false; |
726 } |
804 } |
727 } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) { |
805 } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) { |
728 // If no callback exists, look for the old-style single_text and multiple_text arguments. |
806 // If no callback exists, look for the old-style single_text and multiple_text arguments. |
729 $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] ); |
807 $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] ); |
730 } else { |
808 } else { |
731 // This is the default for when no callback, plural, or argument is passed in. |
809 // This is the default for when no callback, plural, or argument is passed in. |
732 $translate_nooped_plural = _n_noop( '%s topic', '%s topics' ); |
810 $translate_nooped_plural = _n_noop( '%s item', '%s items' ); |
733 } |
811 } |
734 |
812 |
735 /** |
813 /** |
736 * Filter how the items in a tag cloud are sorted. |
814 * Filters how the items in a tag cloud are sorted. |
737 * |
815 * |
738 * @since 2.8.0 |
816 * @since 2.8.0 |
739 * |
817 * |
740 * @param array $tags Ordered array of terms. |
818 * @param array $tags Ordered array of terms. |
741 * @param array $args An array of tag cloud arguments. |
819 * @param array $args An array of tag cloud arguments. |
782 $font_spread = $args['largest'] - $args['smallest']; |
860 $font_spread = $args['largest'] - $args['smallest']; |
783 if ( $font_spread < 0 ) |
861 if ( $font_spread < 0 ) |
784 $font_spread = 1; |
862 $font_spread = 1; |
785 $font_step = $font_spread / $spread; |
863 $font_step = $font_spread / $spread; |
786 |
864 |
787 $a = array(); |
865 $aria_label = false; |
788 |
866 /* |
|
867 * Determine whether to output an 'aria-label' attribute with the tag name and count. |
|
868 * When tags have a different font size, they visually convey an important information |
|
869 * that should be available to assistive technologies too. On the other hand, sometimes |
|
870 * themes set up the Tag Cloud to display all tags with the same font size (setting |
|
871 * the 'smallest' and 'largest' arguments to the same value). |
|
872 * In order to always serve the same content to all users, the 'aria-label' gets printed out: |
|
873 * - when tags have a different size |
|
874 * - when the tag count is displayed (for example when users check the checkbox in the |
|
875 * Tag Cloud widget), regardless of the tags font size |
|
876 */ |
|
877 if ( $args['show_count'] || 0 !== $font_spread ) { |
|
878 $aria_label = true; |
|
879 } |
|
880 |
|
881 // Assemble the data that will be used to generate the tag cloud markup. |
|
882 $tags_data = array(); |
789 foreach ( $tags as $key => $tag ) { |
883 foreach ( $tags as $key => $tag ) { |
|
884 $tag_id = isset( $tag->id ) ? $tag->id : $key; |
|
885 |
790 $count = $counts[ $key ]; |
886 $count = $counts[ $key ]; |
791 $real_count = $real_counts[ $key ]; |
887 $real_count = $real_counts[ $key ]; |
792 $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#'; |
|
793 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key; |
|
794 $tag_name = $tags[ $key ]->name; |
|
795 |
888 |
796 if ( $translate_nooped_plural ) { |
889 if ( $translate_nooped_plural ) { |
797 $title_attribute = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) ); |
890 $formatted_count = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) ); |
798 } else { |
891 } else { |
799 $title_attribute = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args ); |
892 $formatted_count = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args ); |
800 } |
893 } |
801 |
894 |
802 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $title_attribute ) . "' style='font-size: " . |
895 $tags_data[] = array( |
803 str_replace( ',', '.', ( $args['smallest'] + ( ( $count - $min_count ) * $font_step ) ) ) |
896 'id' => $tag_id, |
804 . $args['unit'] . ";'>$tag_name</a>"; |
897 'url' => '#' != $tag->link ? $tag->link : '#', |
|
898 'role' => '#' != $tag->link ? '' : ' role="button"', |
|
899 'name' => $tag->name, |
|
900 'formatted_count' => $formatted_count, |
|
901 'slug' => $tag->slug, |
|
902 'real_count' => $real_count, |
|
903 'class' => 'tag-cloud-link tag-link-' . $tag_id, |
|
904 'font_size' => $args['smallest'] + ( $count - $min_count ) * $font_step, |
|
905 'aria_label' => $aria_label ? sprintf( ' aria-label="%1$s (%2$s)"', esc_attr( $tag->name ), esc_attr( $formatted_count ) ) : '', |
|
906 'show_count' => $args['show_count'] ? '<span class="tag-link-count"> (' . $real_count . ')</span>' : '', |
|
907 ); |
|
908 } |
|
909 |
|
910 /** |
|
911 * Filters the data used to generate the tag cloud. |
|
912 * |
|
913 * @since 4.3.0 |
|
914 * |
|
915 * @param array $tags_data An array of term data for term used to generate the tag cloud. |
|
916 */ |
|
917 $tags_data = apply_filters( 'wp_generate_tag_cloud_data', $tags_data ); |
|
918 |
|
919 $a = array(); |
|
920 |
|
921 // Generate the output links array. |
|
922 foreach ( $tags_data as $key => $tag_data ) { |
|
923 $class = $tag_data['class'] . ' tag-link-position-' . ( $key + 1 ); |
|
924 $a[] = sprintf( |
|
925 '<a href="%1$s"%2$s class="%3$s" style="font-size: %4$s;"%5$s>%6$s%7$s</a>', |
|
926 esc_url( $tag_data['url'] ), |
|
927 $tag_data['role'], |
|
928 esc_attr( $class ), |
|
929 esc_attr( str_replace( ',', '.', $tag_data['font_size'] ) . $args['unit'] ), |
|
930 $tag_data['aria_label'], |
|
931 esc_html( $tag_data['name'] ), |
|
932 $tag_data['show_count'] |
|
933 ); |
805 } |
934 } |
806 |
935 |
807 switch ( $args['format'] ) { |
936 switch ( $args['format'] ) { |
808 case 'array' : |
937 case 'array' : |
809 $return =& $a; |
938 $return =& $a; |
810 break; |
939 break; |
811 case 'list' : |
940 case 'list' : |
812 $return = "<ul class='wp-tag-cloud'>\n\t<li>"; |
941 /* |
|
942 * Force role="list", as some browsers (sic: Safari 10) don't expose to assistive |
|
943 * technologies the default role when the list is styled with `list-style: none`. |
|
944 * Note: this is redundant but doesn't harm. |
|
945 */ |
|
946 $return = "<ul class='wp-tag-cloud' role='list'>\n\t<li>"; |
813 $return .= join( "</li>\n\t<li>", $a ); |
947 $return .= join( "</li>\n\t<li>", $a ); |
814 $return .= "</li>\n</ul>\n"; |
948 $return .= "</li>\n</ul>\n"; |
815 break; |
949 break; |
816 default : |
950 default : |
817 $return = join( $args['separator'], $a ); |
951 $return = join( $args['separator'], $a ); |
818 break; |
952 break; |
819 } |
953 } |
820 |
954 |
821 if ( $args['filter'] ) { |
955 if ( $args['filter'] ) { |
822 /** |
956 /** |
823 * Filter the generated output of a tag cloud. |
957 * Filters the generated output of a tag cloud. |
824 * |
958 * |
825 * The filter is only evaluated if a true value is passed |
959 * The filter is only evaluated if a true value is passed |
826 * to the $filter argument in wp_generate_tag_cloud(). |
960 * to the $filter argument in wp_generate_tag_cloud(). |
827 * |
961 * |
828 * @since 2.3.0 |
962 * @since 2.3.0 |
870 * Retrieve HTML list content for category list. |
1017 * Retrieve HTML list content for category list. |
871 * |
1018 * |
872 * @uses Walker_Category to create HTML list content. |
1019 * @uses Walker_Category to create HTML list content. |
873 * @since 2.1.0 |
1020 * @since 2.1.0 |
874 * @see Walker_Category::walk() for parameters and return description. |
1021 * @see Walker_Category::walk() for parameters and return description. |
|
1022 * @return string |
875 */ |
1023 */ |
876 function walk_category_tree() { |
1024 function walk_category_tree() { |
877 $args = func_get_args(); |
1025 $args = func_get_args(); |
878 // the user's options are the third parameter |
1026 // the user's options are the third parameter |
879 if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) { |
1027 if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) { |
880 $walker = new Walker_Category; |
1028 $walker = new Walker_Category; |
881 } else { |
1029 } else { |
882 $walker = $args[2]['walker']; |
1030 $walker = $args[2]['walker']; |
883 } |
1031 } |
884 return call_user_func_array(array( &$walker, 'walk' ), $args ); |
1032 return call_user_func_array( array( $walker, 'walk' ), $args ); |
885 } |
1033 } |
886 |
1034 |
887 /** |
1035 /** |
888 * Retrieve HTML dropdown (select) content for category list. |
1036 * Retrieve HTML dropdown (select) content for category list. |
889 * |
1037 * |
890 * @uses Walker_CategoryDropdown to create HTML dropdown content. |
1038 * @uses Walker_CategoryDropdown to create HTML dropdown content. |
891 * @since 2.1.0 |
1039 * @since 2.1.0 |
892 * @see Walker_CategoryDropdown::walk() for parameters and return description. |
1040 * @see Walker_CategoryDropdown::walk() for parameters and return description. |
|
1041 * @return string |
893 */ |
1042 */ |
894 function walk_category_dropdown_tree() { |
1043 function walk_category_dropdown_tree() { |
895 $args = func_get_args(); |
1044 $args = func_get_args(); |
896 // the user's options are the third parameter |
1045 // the user's options are the third parameter |
897 if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') ) |
1046 if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) { |
898 $walker = new Walker_CategoryDropdown; |
1047 $walker = new Walker_CategoryDropdown; |
899 else |
1048 } else { |
900 $walker = $args[2]['walker']; |
1049 $walker = $args[2]['walker']; |
901 |
1050 } |
902 return call_user_func_array(array( &$walker, 'walk' ), $args ); |
1051 return call_user_func_array( array( $walker, 'walk' ), $args ); |
903 } |
|
904 |
|
905 /** |
|
906 * Create HTML list of categories. |
|
907 * |
|
908 * @package WordPress |
|
909 * @since 2.1.0 |
|
910 * @uses Walker |
|
911 */ |
|
912 class Walker_Category extends Walker { |
|
913 /** |
|
914 * What the class handles. |
|
915 * |
|
916 * @see Walker::$tree_type |
|
917 * @since 2.1.0 |
|
918 * @var string |
|
919 */ |
|
920 public $tree_type = 'category'; |
|
921 |
|
922 /** |
|
923 * Database fields to use. |
|
924 * |
|
925 * @see Walker::$db_fields |
|
926 * @since 2.1.0 |
|
927 * @todo Decouple this |
|
928 * @var array |
|
929 */ |
|
930 public $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); |
|
931 |
|
932 /** |
|
933 * Starts the list before the elements are added. |
|
934 * |
|
935 * @see Walker::start_lvl() |
|
936 * |
|
937 * @since 2.1.0 |
|
938 * |
|
939 * @param string $output Passed by reference. Used to append additional content. |
|
940 * @param int $depth Depth of category. Used for tab indentation. |
|
941 * @param array $args An array of arguments. Will only append content if style argument value is 'list'. |
|
942 * @see wp_list_categories() |
|
943 */ |
|
944 public function start_lvl( &$output, $depth = 0, $args = array() ) { |
|
945 if ( 'list' != $args['style'] ) |
|
946 return; |
|
947 |
|
948 $indent = str_repeat("\t", $depth); |
|
949 $output .= "$indent<ul class='children'>\n"; |
|
950 } |
|
951 |
|
952 /** |
|
953 * Ends the list of after the elements are added. |
|
954 * |
|
955 * @see Walker::end_lvl() |
|
956 * |
|
957 * @since 2.1.0 |
|
958 * |
|
959 * @param string $output Passed by reference. Used to append additional content. |
|
960 * @param int $depth Depth of category. Used for tab indentation. |
|
961 * @param array $args An array of arguments. Will only append content if style argument value is 'list'. |
|
962 * @wsee wp_list_categories() |
|
963 */ |
|
964 public function end_lvl( &$output, $depth = 0, $args = array() ) { |
|
965 if ( 'list' != $args['style'] ) |
|
966 return; |
|
967 |
|
968 $indent = str_repeat("\t", $depth); |
|
969 $output .= "$indent</ul>\n"; |
|
970 } |
|
971 |
|
972 /** |
|
973 * Start the element output. |
|
974 * |
|
975 * @see Walker::start_el() |
|
976 * |
|
977 * @since 2.1.0 |
|
978 * |
|
979 * @param string $output Passed by reference. Used to append additional content. |
|
980 * @param object $category Category data object. |
|
981 * @param int $depth Depth of category in reference to parents. Default 0. |
|
982 * @param array $args An array of arguments. @see wp_list_categories() |
|
983 * @param int $id ID of the current category. |
|
984 */ |
|
985 public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { |
|
986 /** This filter is documented in wp-includes/category-template.php */ |
|
987 $cat_name = apply_filters( |
|
988 'list_cats', |
|
989 esc_attr( $category->name ), |
|
990 $category |
|
991 ); |
|
992 |
|
993 // Don't generate an element if the category name is empty. |
|
994 if ( ! $cat_name ) { |
|
995 return; |
|
996 } |
|
997 |
|
998 $link = '<a href="' . esc_url( get_term_link( $category ) ) . '" '; |
|
999 if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) { |
|
1000 /** |
|
1001 * Filter the category description for display. |
|
1002 * |
|
1003 * @since 1.2.0 |
|
1004 * |
|
1005 * @param string $description Category description. |
|
1006 * @param object $category Category object. |
|
1007 */ |
|
1008 $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"'; |
|
1009 } |
|
1010 |
|
1011 $link .= '>'; |
|
1012 $link .= $cat_name . '</a>'; |
|
1013 |
|
1014 if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) { |
|
1015 $link .= ' '; |
|
1016 |
|
1017 if ( empty( $args['feed_image'] ) ) { |
|
1018 $link .= '('; |
|
1019 } |
|
1020 |
|
1021 $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"'; |
|
1022 |
|
1023 if ( empty( $args['feed'] ) ) { |
|
1024 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"'; |
|
1025 } else { |
|
1026 $alt = ' alt="' . $args['feed'] . '"'; |
|
1027 $name = $args['feed']; |
|
1028 $link .= empty( $args['title'] ) ? '' : $args['title']; |
|
1029 } |
|
1030 |
|
1031 $link .= '>'; |
|
1032 |
|
1033 if ( empty( $args['feed_image'] ) ) { |
|
1034 $link .= $name; |
|
1035 } else { |
|
1036 $link .= "<img src='" . $args['feed_image'] . "'$alt" . ' />'; |
|
1037 } |
|
1038 $link .= '</a>'; |
|
1039 |
|
1040 if ( empty( $args['feed_image'] ) ) { |
|
1041 $link .= ')'; |
|
1042 } |
|
1043 } |
|
1044 |
|
1045 if ( ! empty( $args['show_count'] ) ) { |
|
1046 $link .= ' (' . number_format_i18n( $category->count ) . ')'; |
|
1047 } |
|
1048 if ( 'list' == $args['style'] ) { |
|
1049 $output .= "\t<li"; |
|
1050 $css_classes = array( |
|
1051 'cat-item', |
|
1052 'cat-item-' . $category->term_id, |
|
1053 ); |
|
1054 |
|
1055 if ( ! empty( $args['current_category'] ) ) { |
|
1056 $_current_category = get_term( $args['current_category'], $category->taxonomy ); |
|
1057 if ( $category->term_id == $args['current_category'] ) { |
|
1058 $css_classes[] = 'current-cat'; |
|
1059 } elseif ( $category->term_id == $_current_category->parent ) { |
|
1060 $css_classes[] = 'current-cat-parent'; |
|
1061 } |
|
1062 } |
|
1063 |
|
1064 /** |
|
1065 * Filter the list of CSS classes to include with each category in the list. |
|
1066 * |
|
1067 * @since 4.2.0 |
|
1068 * |
|
1069 * @see wp_list_categories() |
|
1070 * |
|
1071 * @param array $css_classes An array of CSS classes to be applied to each list item. |
|
1072 * @param object $category Category data object. |
|
1073 * @param int $depth Depth of page, used for padding. |
|
1074 * @param array $args An array of wp_list_categories() arguments. |
|
1075 */ |
|
1076 $css_classes = implode( ' ', apply_filters( 'category_css_class', $css_classes, $category, $depth, $args ) ); |
|
1077 |
|
1078 $output .= ' class="' . $css_classes . '"'; |
|
1079 $output .= ">$link\n"; |
|
1080 } else { |
|
1081 $output .= "\t$link<br />\n"; |
|
1082 } |
|
1083 } |
|
1084 |
|
1085 /** |
|
1086 * Ends the element output, if needed. |
|
1087 * |
|
1088 * @see Walker::end_el() |
|
1089 * |
|
1090 * @since 2.1.0 |
|
1091 * |
|
1092 * @param string $output Passed by reference. Used to append additional content. |
|
1093 * @param object $page Not used. |
|
1094 * @param int $depth Depth of category. Not used. |
|
1095 * @param array $args An array of arguments. Only uses 'list' for whether should append to output. @see wp_list_categories() |
|
1096 */ |
|
1097 public function end_el( &$output, $page, $depth = 0, $args = array() ) { |
|
1098 if ( 'list' != $args['style'] ) |
|
1099 return; |
|
1100 |
|
1101 $output .= "</li>\n"; |
|
1102 } |
|
1103 |
|
1104 } |
|
1105 |
|
1106 /** |
|
1107 * Create HTML dropdown list of Categories. |
|
1108 * |
|
1109 * @package WordPress |
|
1110 * @since 2.1.0 |
|
1111 * @uses Walker |
|
1112 */ |
|
1113 class Walker_CategoryDropdown extends Walker { |
|
1114 /** |
|
1115 * @see Walker::$tree_type |
|
1116 * @since 2.1.0 |
|
1117 * @var string |
|
1118 */ |
|
1119 public $tree_type = 'category'; |
|
1120 |
|
1121 /** |
|
1122 * @see Walker::$db_fields |
|
1123 * @since 2.1.0 |
|
1124 * @todo Decouple this |
|
1125 * @var array |
|
1126 */ |
|
1127 public $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); |
|
1128 |
|
1129 /** |
|
1130 * Start the element output. |
|
1131 * |
|
1132 * @see Walker::start_el() |
|
1133 * @since 2.1.0 |
|
1134 * |
|
1135 * @param string $output Passed by reference. Used to append additional content. |
|
1136 * @param object $category Category data object. |
|
1137 * @param int $depth Depth of category. Used for padding. |
|
1138 * @param array $args Uses 'selected', 'show_count', and 'value_field' keys, if they exist. |
|
1139 * See {@see wp_dropdown_categories()}. |
|
1140 */ |
|
1141 public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { |
|
1142 $pad = str_repeat(' ', $depth * 3); |
|
1143 |
|
1144 /** This filter is documented in wp-includes/category-template.php */ |
|
1145 $cat_name = apply_filters( 'list_cats', $category->name, $category ); |
|
1146 |
|
1147 if ( ! isset( $args['value_field'] ) || ! isset( $category->{$args['value_field']} ) ) { |
|
1148 $args['value_field'] = 'term_id'; |
|
1149 } |
|
1150 |
|
1151 $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$args['value_field']} ) . "\""; |
|
1152 |
|
1153 if ( $category->term_id == $args['selected'] ) |
|
1154 $output .= ' selected="selected"'; |
|
1155 $output .= '>'; |
|
1156 $output .= $pad.$cat_name; |
|
1157 if ( $args['show_count'] ) |
|
1158 $output .= ' ('. number_format_i18n( $category->count ) .')'; |
|
1159 $output .= "</option>\n"; |
|
1160 } |
|
1161 } |
1052 } |
1162 |
1053 |
1163 // |
1054 // |
1164 // Tags |
1055 // Tags |
1165 // |
1056 // |
1263 |
1151 |
1264 /** |
1152 /** |
1265 * Retrieve term description. |
1153 * Retrieve term description. |
1266 * |
1154 * |
1267 * @since 2.8.0 |
1155 * @since 2.8.0 |
1268 * |
1156 * @since 4.9.2 The `$taxonomy` parameter was deprecated. |
1269 * @param int $term Optional. Term ID. Will use global term ID by default. |
1157 * |
1270 * @param string $taxonomy Optional taxonomy name. Defaults to 'post_tag'. |
1158 * @param int $term Optional. Term ID. Will use global term ID by default. |
|
1159 * @param null $deprecated Deprecated argument. |
1271 * @return string Term description, available. |
1160 * @return string Term description, available. |
1272 */ |
1161 */ |
1273 function term_description( $term = 0, $taxonomy = 'post_tag' ) { |
1162 function term_description( $term = 0, $deprecated = null ) { |
1274 if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) { |
1163 if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) { |
1275 $term = get_queried_object(); |
1164 $term = get_queried_object(); |
1276 if ( $term ) { |
1165 if ( $term ) { |
1277 $taxonomy = $term->taxonomy; |
|
1278 $term = $term->term_id; |
1166 $term = $term->term_id; |
1279 } |
1167 } |
1280 } |
1168 } |
1281 $description = get_term_field( 'description', $term, $taxonomy ); |
1169 $description = get_term_field( 'description', $term ); |
1282 return is_wp_error( $description ) ? '' : $description; |
1170 return is_wp_error( $description ) ? '' : $description; |
1283 } |
1171 } |
1284 |
1172 |
1285 /** |
1173 /** |
1286 * Retrieve the terms of the taxonomy that are attached to the post. |
1174 * Retrieve the terms of the taxonomy that are attached to the post. |
1287 * |
1175 * |
1288 * @since 2.5.0 |
1176 * @since 2.5.0 |
1289 * |
1177 * |
1290 * @param int|object $post Post ID or object. |
1178 * @param int|object $post Post ID or object. |
1291 * @param string $taxonomy Taxonomy name. |
1179 * @param string $taxonomy Taxonomy name. |
1292 * @return array|bool|WP_Error Array of term objects on success, false if there are no terms |
1180 * @return array|false|WP_Error Array of WP_Term objects on success, false if there are no terms |
1293 * or the post does not exist, WP_Error on failure. |
1181 * or the post does not exist, WP_Error on failure. |
1294 */ |
1182 */ |
1295 function get_the_terms( $post, $taxonomy ) { |
1183 function get_the_terms( $post, $taxonomy ) { |
1296 if ( ! $post = get_post( $post ) ) |
1184 if ( ! $post = get_post( $post ) ) |
1297 return false; |
1185 return false; |
1298 |
1186 |
1299 $terms = get_object_term_cache( $post->ID, $taxonomy ); |
1187 $terms = get_object_term_cache( $post->ID, $taxonomy ); |
1300 if ( false === $terms ) { |
1188 if ( false === $terms ) { |
1301 $terms = wp_get_object_terms( $post->ID, $taxonomy ); |
1189 $terms = wp_get_object_terms( $post->ID, $taxonomy ); |
1302 wp_cache_add($post->ID, $terms, $taxonomy . '_relationships'); |
1190 if ( ! is_wp_error( $terms ) ) { |
|
1191 $term_ids = wp_list_pluck( $terms, 'term_id' ); |
|
1192 wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' ); |
|
1193 } |
1303 } |
1194 } |
1304 |
1195 |
1305 /** |
1196 /** |
1306 * Filter the list of terms attached to the given post. |
1197 * Filters the list of terms attached to the given post. |
1307 * |
1198 * |
1308 * @since 3.1.0 |
1199 * @since 3.1.0 |
1309 * |
1200 * |
1310 * @param array|WP_Error $terms List of attached terms, or WP_Error on failure. |
1201 * @param array|WP_Error $terms List of attached terms, or WP_Error on failure. |
1311 * @param int $post_id Post ID. |
1202 * @param int $post_id Post ID. |
1349 } |
1240 } |
1350 $links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>'; |
1241 $links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>'; |
1351 } |
1242 } |
1352 |
1243 |
1353 /** |
1244 /** |
1354 * Filter the term links for a given taxonomy. |
1245 * Filters the term links for a given taxonomy. |
1355 * |
1246 * |
1356 * The dynamic portion of the filter name, `$taxonomy`, refers |
1247 * The dynamic portion of the filter name, `$taxonomy`, refers |
1357 * to the taxonomy slug. |
1248 * to the taxonomy slug. |
1358 * |
1249 * |
1359 * @since 2.5.0 |
1250 * @since 2.5.0 |
1360 * |
1251 * |
1361 * @param array $links An array of term links. |
1252 * @param array $links An array of term links. |
1362 */ |
1253 */ |
1363 $term_links = apply_filters( "term_links-$taxonomy", $links ); |
1254 $term_links = apply_filters( "term_links-{$taxonomy}", $links ); |
1364 |
1255 |
1365 return $before . join( $sep, $term_links ) . $after; |
1256 return $before . join( $sep, $term_links ) . $after; |
|
1257 } |
|
1258 |
|
1259 /** |
|
1260 * Retrieve term parents with separator. |
|
1261 * |
|
1262 * @since 4.8.0 |
|
1263 * |
|
1264 * @param int $term_id Term ID. |
|
1265 * @param string $taxonomy Taxonomy name. |
|
1266 * @param string|array $args { |
|
1267 * Array of optional arguments. |
|
1268 * |
|
1269 * @type string $format Use term names or slugs for display. Accepts 'name' or 'slug'. |
|
1270 * Default 'name'. |
|
1271 * @type string $separator Separator for between the terms. Default '/'. |
|
1272 * @type bool $link Whether to format as a link. Default true. |
|
1273 * @type bool $inclusive Include the term to get the parents for. Default true. |
|
1274 * } |
|
1275 * @return string|WP_Error A list of term parents on success, WP_Error or empty string on failure. |
|
1276 */ |
|
1277 function get_term_parents_list( $term_id, $taxonomy, $args = array() ) { |
|
1278 $list = ''; |
|
1279 $term = get_term( $term_id, $taxonomy ); |
|
1280 |
|
1281 if ( is_wp_error( $term ) ) { |
|
1282 return $term; |
|
1283 } |
|
1284 |
|
1285 if ( ! $term ) { |
|
1286 return $list; |
|
1287 } |
|
1288 |
|
1289 $term_id = $term->term_id; |
|
1290 |
|
1291 $defaults = array( |
|
1292 'format' => 'name', |
|
1293 'separator' => '/', |
|
1294 'link' => true, |
|
1295 'inclusive' => true, |
|
1296 ); |
|
1297 |
|
1298 $args = wp_parse_args( $args, $defaults ); |
|
1299 |
|
1300 foreach ( array( 'link', 'inclusive' ) as $bool ) { |
|
1301 $args[ $bool ] = wp_validate_boolean( $args[ $bool ] ); |
|
1302 } |
|
1303 |
|
1304 $parents = get_ancestors( $term_id, $taxonomy, 'taxonomy' ); |
|
1305 |
|
1306 if ( $args['inclusive'] ) { |
|
1307 array_unshift( $parents, $term_id ); |
|
1308 } |
|
1309 |
|
1310 foreach ( array_reverse( $parents ) as $term_id ) { |
|
1311 $parent = get_term( $term_id, $taxonomy ); |
|
1312 $name = ( 'slug' === $args['format'] ) ? $parent->slug : $parent->name; |
|
1313 |
|
1314 if ( $args['link'] ) { |
|
1315 $list .= '<a href="' . esc_url( get_term_link( $parent->term_id, $taxonomy ) ) . '">' . $name . '</a>' . $args['separator']; |
|
1316 } else { |
|
1317 $list .= $name . $args['separator']; |
|
1318 } |
|
1319 } |
|
1320 |
|
1321 return $list; |
1366 } |
1322 } |
1367 |
1323 |
1368 /** |
1324 /** |
1369 * Display the terms in a list. |
1325 * Display the terms in a list. |
1370 * |
1326 * |