29 |
30 |
30 return $category; |
31 return $category; |
31 } |
32 } |
32 |
33 |
33 /** |
34 /** |
34 * Retrieve category parents with separator. |
35 * Retrieves category parents with separator. |
35 * |
36 * |
36 * @since 1.2.0 |
37 * @since 1.2.0 |
37 * @since 4.8.0 The `$visited` parameter was deprecated and renamed to `$deprecated`. |
38 * @since 4.8.0 The `$visited` parameter was deprecated and renamed to `$deprecated`. |
38 * |
39 * |
39 * @param int $id Category ID. |
40 * @param int $category_id Category ID. |
40 * @param bool $link Optional, default is false. Whether to format with link. |
41 * @param bool $link Optional. Whether to format with link. Default false. |
41 * @param string $separator Optional, default is '/'. How to separate categories. |
42 * @param string $separator Optional. How to separate categories. Default '/'. |
42 * @param bool $nicename Optional, default is false. Whether to use nice name for display. |
43 * @param bool $nicename Optional. Whether to use nice name for display. Default false. |
43 * @param array $deprecated Not used. |
44 * @param array $deprecated Not used. |
44 * @return string|WP_Error A list of category parents on success, WP_Error on failure. |
45 * @return string|WP_Error A list of category parents on success, WP_Error on failure. |
45 */ |
46 */ |
46 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $deprecated = array() ) { |
47 function get_category_parents( $category_id, $link = false, $separator = '/', $nicename = false, $deprecated = array() ) { |
47 |
48 |
48 if ( ! empty( $deprecated ) ) { |
49 if ( ! empty( $deprecated ) ) { |
49 _deprecated_argument( __FUNCTION__, '4.8.0' ); |
50 _deprecated_argument( __FUNCTION__, '4.8.0' ); |
50 } |
51 } |
51 |
52 |
55 'separator' => $separator, |
56 'separator' => $separator, |
56 'link' => $link, |
57 'link' => $link, |
57 'format' => $format, |
58 'format' => $format, |
58 ); |
59 ); |
59 |
60 |
60 return get_term_parents_list( $id, 'category', $args ); |
61 return get_term_parents_list( $category_id, 'category', $args ); |
61 } |
62 } |
62 |
63 |
63 /** |
64 /** |
64 * Retrieve post categories. |
65 * Retrieves post categories. |
65 * |
66 * |
66 * This tag may be used outside The Loop by passing a post id as the parameter. |
67 * This tag may be used outside The Loop by passing a post ID as the parameter. |
67 * |
68 * |
68 * Note: This function only returns results from the default "category" taxonomy. |
69 * Note: This function only returns results from the default "category" taxonomy. |
69 * For custom taxonomies use get_the_terms(). |
70 * For custom taxonomies use get_the_terms(). |
70 * |
71 * |
71 * @since 0.71 |
72 * @since 0.71 |
72 * |
73 * |
73 * @param int $id Optional, default to current post ID. The post ID. |
74 * @param int $post_id Optional. The post ID. Defaults to current post ID. |
74 * @return WP_Term[] Array of WP_Term objects, one for each category assigned to the post. |
75 * @return WP_Term[] Array of WP_Term objects, one for each category assigned to the post. |
75 */ |
76 */ |
76 function get_the_category( $id = false ) { |
77 function get_the_category( $post_id = false ) { |
77 $categories = get_the_terms( $id, 'category' ); |
78 $categories = get_the_terms( $post_id, 'category' ); |
78 if ( ! $categories || is_wp_error( $categories ) ) { |
79 if ( ! $categories || is_wp_error( $categories ) ) { |
79 $categories = array(); |
80 $categories = array(); |
80 } |
81 } |
81 |
82 |
82 $categories = array_values( $categories ); |
83 $categories = array_values( $categories ); |
115 |
116 |
116 return ( $category ) ? $category->name : ''; |
117 return ( $category ) ? $category->name : ''; |
117 } |
118 } |
118 |
119 |
119 /** |
120 /** |
120 * Retrieve category list for a post in either HTML list or custom format. |
121 * Retrieves category list for a post in either HTML list or custom format. |
|
122 * |
|
123 * Generally used for quick, delimited (e.g. comma-separated) lists of categories, |
|
124 * as part of a post entry meta. |
|
125 * |
|
126 * For a more powerful, list-based function, see wp_list_categories(). |
121 * |
127 * |
122 * @since 1.5.1 |
128 * @since 1.5.1 |
123 * |
129 * |
124 * @global WP_Rewrite $wp_rewrite |
130 * @see wp_list_categories() |
|
131 * |
|
132 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
125 * |
133 * |
126 * @param string $separator Optional. Separator between the categories. By default, the links are placed |
134 * @param string $separator Optional. Separator between the categories. By default, the links are placed |
127 * in an unordered list. An empty string will result in the default behavior. |
135 * in an unordered list. An empty string will result in the default behavior. |
128 * @param string $parents Optional. How to display the parents. |
136 * @param string $parents Optional. How to display the parents. |
129 * @param int $post_id Optional. Post ID to retrieve categories. |
137 * @param int $post_id Optional. Post ID to retrieve categories. |
130 * @return string |
138 * @return string Category list for a post. |
131 */ |
139 */ |
132 function get_the_category_list( $separator = '', $parents = '', $post_id = false ) { |
140 function get_the_category_list( $separator = '', $parents = '', $post_id = false ) { |
133 global $wp_rewrite; |
141 global $wp_rewrite; |
|
142 |
134 if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) ) { |
143 if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) ) { |
135 /** This filter is documented in wp-includes/category-template.php */ |
144 /** This filter is documented in wp-includes/category-template.php */ |
136 return apply_filters( 'the_category', '', $separator, $parents ); |
145 return apply_filters( 'the_category', '', $separator, $parents ); |
137 } |
146 } |
138 |
147 |
249 |
259 |
250 return has_category( $category, $post ); |
260 return has_category( $category, $post ); |
251 } |
261 } |
252 |
262 |
253 /** |
263 /** |
254 * Display category list for a post in either HTML list or custom format. |
264 * Displays category list for a post in either HTML list or custom format. |
255 * |
265 * |
256 * @since 0.71 |
266 * @since 0.71 |
257 * |
267 * |
258 * @param string $separator Optional. Separator between the categories. By default, the links are placed |
268 * @param string $separator Optional. Separator between the categories. By default, the links are placed |
259 * in an unordered list. An empty string will result in the default behavior. |
269 * in an unordered list. An empty string will result in the default behavior. |
260 * @param string $parents Optional. How to display the parents. |
270 * @param string $parents Optional. How to display the parents. |
261 * @param int $post_id Optional. Post ID to retrieve categories. |
271 * @param int $post_id Optional. Post ID to retrieve categories. |
262 */ |
272 */ |
263 function the_category( $separator = '', $parents = '', $post_id = false ) { |
273 function the_category( $separator = '', $parents = '', $post_id = false ) { |
264 echo get_the_category_list( $separator, $parents, $post_id ); |
274 echo get_the_category_list( $separator, $parents, $post_id ); |
265 } |
275 } |
266 |
276 |
267 /** |
277 /** |
268 * Retrieve category description. |
278 * Retrieves category description. |
269 * |
279 * |
270 * @since 1.0.0 |
280 * @since 1.0.0 |
271 * |
281 * |
272 * @param int $category Optional. Category ID. Will use global category ID by default. |
282 * @param int $category Optional. Category ID. Defaults to the current category ID. |
273 * @return string Category description, available. |
283 * @return string Category description, if available. |
274 */ |
284 */ |
275 function category_description( $category = 0 ) { |
285 function category_description( $category = 0 ) { |
276 return term_description( $category ); |
286 return term_description( $category ); |
277 } |
287 } |
278 |
288 |
279 /** |
289 /** |
280 * Display or retrieve the HTML dropdown list of categories. |
290 * Displays or retrieves the HTML dropdown list of categories. |
281 * |
291 * |
282 * The 'hierarchical' argument, which is disabled by default, will override the |
292 * The 'hierarchical' argument, which is disabled by default, will override the |
283 * depth argument, unless it is true. When the argument is false, it will |
293 * depth argument, unless it is true. When the argument is false, it will |
284 * display all of the categories. When it is enabled it will use the value in |
294 * display all of the categories. When it is enabled it will use the value in |
285 * the 'depth' argument. |
295 * the 'depth' argument. |
286 * |
296 * |
287 * @since 2.1.0 |
297 * @since 2.1.0 |
288 * @since 4.2.0 Introduced the `value_field` argument. |
298 * @since 4.2.0 Introduced the `value_field` argument. |
289 * @since 4.6.0 Introduced the `required` argument. |
299 * @since 4.6.0 Introduced the `required` argument. |
290 * |
300 * |
291 * @param string|array $args { |
301 * @param array|string $args { |
292 * Optional. Array or string of arguments to generate a categories drop-down element. See WP_Term_Query::__construct() |
302 * Optional. Array or string of arguments to generate a categories drop-down element. See WP_Term_Query::__construct() |
293 * for information on additional accepted arguments. |
303 * for information on additional accepted arguments. |
294 * |
304 * |
295 * @type string $show_option_all Text to display for showing all categories. Default empty. |
305 * @type string $show_option_all Text to display for showing all categories. Default empty. |
296 * @type string $show_option_none Text to display for showing no categories. Default empty. |
306 * @type string $show_option_none Text to display for showing no categories. Default empty. |
349 ); |
359 ); |
350 |
360 |
351 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0; |
361 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0; |
352 |
362 |
353 // Back compat. |
363 // Back compat. |
354 if ( isset( $args['type'] ) && 'link' == $args['type'] ) { |
364 if ( isset( $args['type'] ) && 'link' === $args['type'] ) { |
355 _deprecated_argument( |
365 _deprecated_argument( |
356 __FUNCTION__, |
366 __FUNCTION__, |
357 '3.0.0', |
367 '3.0.0', |
358 /* translators: 1: "type => link", 2: "taxonomy => link_category" */ |
|
359 sprintf( |
368 sprintf( |
|
369 /* translators: 1: "type => link", 2: "taxonomy => link_category" */ |
360 __( '%1$s is deprecated. Use %2$s instead.' ), |
370 __( '%1$s is deprecated. Use %2$s instead.' ), |
361 '<code>type => link</code>', |
371 '<code>type => link</code>', |
362 '<code>taxonomy => link_category</code>' |
372 '<code>taxonomy => link_category</code>' |
363 ) |
373 ) |
364 ); |
374 ); |
365 $args['taxonomy'] = 'link_category'; |
375 $args['taxonomy'] = 'link_category'; |
366 } |
376 } |
367 |
377 |
368 $r = wp_parse_args( $args, $defaults ); |
378 // Parse incoming $args into an array and merge it with $defaults. |
369 $option_none_value = $r['option_none_value']; |
379 $parsed_args = wp_parse_args( $args, $defaults ); |
370 |
380 |
371 if ( ! isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) { |
381 $option_none_value = $parsed_args['option_none_value']; |
372 $r['pad_counts'] = true; |
382 |
373 } |
383 if ( ! isset( $parsed_args['pad_counts'] ) && $parsed_args['show_count'] && $parsed_args['hierarchical'] ) { |
374 |
384 $parsed_args['pad_counts'] = true; |
375 $tab_index = $r['tab_index']; |
385 } |
|
386 |
|
387 $tab_index = $parsed_args['tab_index']; |
376 |
388 |
377 $tab_index_attribute = ''; |
389 $tab_index_attribute = ''; |
378 if ( (int) $tab_index > 0 ) { |
390 if ( (int) $tab_index > 0 ) { |
379 $tab_index_attribute = " tabindex=\"$tab_index\""; |
391 $tab_index_attribute = " tabindex=\"$tab_index\""; |
380 } |
392 } |
381 |
393 |
382 // Avoid clashes with the 'name' param of get_terms(). |
394 // Avoid clashes with the 'name' param of get_terms(). |
383 $get_terms_args = $r; |
395 $get_terms_args = $parsed_args; |
384 unset( $get_terms_args['name'] ); |
396 unset( $get_terms_args['name'] ); |
385 $categories = get_terms( $r['taxonomy'], $get_terms_args ); |
397 $categories = get_terms( $get_terms_args ); |
386 |
398 |
387 $name = esc_attr( $r['name'] ); |
399 $name = esc_attr( $parsed_args['name'] ); |
388 $class = esc_attr( $r['class'] ); |
400 $class = esc_attr( $parsed_args['class'] ); |
389 $id = $r['id'] ? esc_attr( $r['id'] ) : $name; |
401 $id = $parsed_args['id'] ? esc_attr( $parsed_args['id'] ) : $name; |
390 $required = $r['required'] ? 'required' : ''; |
402 $required = $parsed_args['required'] ? 'required' : ''; |
391 |
403 |
392 if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) { |
404 if ( ! $parsed_args['hide_if_empty'] || ! empty( $categories ) ) { |
393 $output = "<select $required name='$name' id='$id' class='$class' $tab_index_attribute>\n"; |
405 $output = "<select $required name='$name' id='$id' class='$class' $tab_index_attribute>\n"; |
394 } else { |
406 } else { |
395 $output = ''; |
407 $output = ''; |
396 } |
408 } |
397 if ( empty( $categories ) && ! $r['hide_if_empty'] && ! empty( $r['show_option_none'] ) ) { |
409 if ( empty( $categories ) && ! $parsed_args['hide_if_empty'] && ! empty( $parsed_args['show_option_none'] ) ) { |
398 |
410 |
399 /** |
411 /** |
400 * Filters a taxonomy drop-down display element. |
412 * Filters a taxonomy drop-down display element. |
401 * |
413 * |
402 * A variety of taxonomy drop-down display elements can be modified |
414 * A variety of taxonomy drop-down display elements can be modified |
409 * @see wp_dropdown_categories() |
421 * @see wp_dropdown_categories() |
410 * |
422 * |
411 * @param string $element Category name. |
423 * @param string $element Category name. |
412 * @param WP_Term|null $category The category object, or null if there's no corresponding category. |
424 * @param WP_Term|null $category The category object, or null if there's no corresponding category. |
413 */ |
425 */ |
414 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'], null ); |
426 $show_option_none = apply_filters( 'list_cats', $parsed_args['show_option_none'], null ); |
415 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "' selected='selected'>$show_option_none</option>\n"; |
427 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "' selected='selected'>$show_option_none</option>\n"; |
416 } |
428 } |
417 |
429 |
418 if ( ! empty( $categories ) ) { |
430 if ( ! empty( $categories ) ) { |
419 |
431 |
420 if ( $r['show_option_all'] ) { |
432 if ( $parsed_args['show_option_all'] ) { |
421 |
433 |
422 /** This filter is documented in wp-includes/category-template.php */ |
434 /** This filter is documented in wp-includes/category-template.php */ |
423 $show_option_all = apply_filters( 'list_cats', $r['show_option_all'], null ); |
435 $show_option_all = apply_filters( 'list_cats', $parsed_args['show_option_all'], null ); |
424 $selected = ( '0' === strval( $r['selected'] ) ) ? " selected='selected'" : ''; |
436 $selected = ( '0' === strval( $parsed_args['selected'] ) ) ? " selected='selected'" : ''; |
425 $output .= "\t<option value='0'$selected>$show_option_all</option>\n"; |
437 $output .= "\t<option value='0'$selected>$show_option_all</option>\n"; |
426 } |
438 } |
427 |
439 |
428 if ( $r['show_option_none'] ) { |
440 if ( $parsed_args['show_option_none'] ) { |
429 |
441 |
430 /** This filter is documented in wp-includes/category-template.php */ |
442 /** This filter is documented in wp-includes/category-template.php */ |
431 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'], null ); |
443 $show_option_none = apply_filters( 'list_cats', $parsed_args['show_option_none'], null ); |
432 $selected = selected( $option_none_value, $r['selected'], false ); |
444 $selected = selected( $option_none_value, $parsed_args['selected'], false ); |
433 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$selected>$show_option_none</option>\n"; |
445 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$selected>$show_option_none</option>\n"; |
434 } |
446 } |
435 |
447 |
436 if ( $r['hierarchical'] ) { |
448 if ( $parsed_args['hierarchical'] ) { |
437 $depth = $r['depth']; // Walk the full depth. |
449 $depth = $parsed_args['depth']; // Walk the full depth. |
438 } else { |
450 } else { |
439 $depth = -1; // Flat. |
451 $depth = -1; // Flat. |
440 } |
452 } |
441 $output .= walk_category_dropdown_tree( $categories, $depth, $r ); |
453 $output .= walk_category_dropdown_tree( $categories, $depth, $parsed_args ); |
442 } |
454 } |
443 |
455 |
444 if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) { |
456 if ( ! $parsed_args['hide_if_empty'] || ! empty( $categories ) ) { |
445 $output .= "</select>\n"; |
457 $output .= "</select>\n"; |
446 } |
458 } |
|
459 |
447 /** |
460 /** |
448 * Filters the taxonomy drop-down output. |
461 * Filters the taxonomy drop-down output. |
449 * |
462 * |
450 * @since 2.1.0 |
463 * @since 2.1.0 |
451 * |
464 * |
452 * @param string $output HTML output. |
465 * @param string $output HTML output. |
453 * @param array $r Arguments used to build the drop-down. |
466 * @param array $parsed_args Arguments used to build the drop-down. |
454 */ |
467 */ |
455 $output = apply_filters( 'wp_dropdown_cats', $output, $r ); |
468 $output = apply_filters( 'wp_dropdown_cats', $output, $parsed_args ); |
456 |
469 |
457 if ( $r['echo'] ) { |
470 if ( $parsed_args['echo'] ) { |
458 echo $output; |
471 echo $output; |
459 } |
472 } |
|
473 |
460 return $output; |
474 return $output; |
461 } |
475 } |
462 |
476 |
463 /** |
477 /** |
464 * Display or retrieve the HTML list of categories. |
478 * Displays or retrieves the HTML list of categories. |
465 * |
479 * |
466 * @since 2.1.0 |
480 * @since 2.1.0 |
467 * @since 4.4.0 Introduced the `hide_title_if_empty` and `separator` arguments. The `current_category` argument was modified to |
481 * @since 4.4.0 Introduced the `hide_title_if_empty` and `separator` arguments. |
468 * optionally accept an array of values. |
482 * @since 4.4.0 The `current_category` argument was modified to optionally accept an array of values. |
469 * |
483 * |
470 * @param string|array $args { |
484 * @param array|string $args { |
471 * Array of optional arguments. |
485 * Array of optional arguments. See get_categories(), get_terms(), and WP_Term_Query::__construct() |
472 * |
486 * for information on additional accepted arguments. |
473 * @type int $child_of Term ID to retrieve child terms of. See get_terms(). Default 0. |
487 * |
474 * @type int|array $current_category ID of category, or array of IDs of categories, that should get the |
488 * @type int|array $current_category ID of category, or array of IDs of categories, that should get the |
475 * 'current-cat' class. Default 0. |
489 * 'current-cat' class. Default 0. |
476 * @type int $depth Category depth. Used for tab indentation. Default 0. |
490 * @type int $depth Category depth. Used for tab indentation. Default 0. |
477 * @type bool|int $echo True to echo markup, false to return it. Default 1. |
491 * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, or their |
|
492 * bool equivalents. Default 1. |
478 * @type array|string $exclude Array or comma/space-separated string of term IDs to exclude. |
493 * @type array|string $exclude Array or comma/space-separated string of term IDs to exclude. |
479 * If `$hierarchical` is true, descendants of `$exclude` terms will also |
494 * If `$hierarchical` is true, descendants of `$exclude` terms will also |
480 * be excluded; see `$exclude_tree`. See get_terms(). |
495 * be excluded; see `$exclude_tree`. See get_terms(). |
481 * Default empty string. |
496 * Default empty string. |
482 * @type array|string $exclude_tree Array or comma/space-separated string of term IDs to exclude, along |
497 * @type array|string $exclude_tree Array or comma/space-separated string of term IDs to exclude, along |
484 * @type string $feed Text to use for the feed link. Default 'Feed for all posts filed |
499 * @type string $feed Text to use for the feed link. Default 'Feed for all posts filed |
485 * under [cat name]'. |
500 * under [cat name]'. |
486 * @type string $feed_image URL of an image to use for the feed link. Default empty string. |
501 * @type string $feed_image URL of an image to use for the feed link. Default empty string. |
487 * @type string $feed_type Feed type. Used to build feed link. See get_term_feed_link(). |
502 * @type string $feed_type Feed type. Used to build feed link. See get_term_feed_link(). |
488 * Default empty string (default feed). |
503 * Default empty string (default feed). |
489 * @type bool|int $hide_empty Whether to hide categories that don't have any posts attached to them. |
|
490 * Default 1. |
|
491 * @type bool $hide_title_if_empty Whether to hide the `$title_li` element if there are no terms in |
504 * @type bool $hide_title_if_empty Whether to hide the `$title_li` element if there are no terms in |
492 * the list. Default false (title will always be shown). |
505 * the list. Default false (title will always be shown). |
493 * @type bool $hierarchical Whether to include terms that have non-empty descendants. |
|
494 * See get_terms(). Default true. |
|
495 * @type string $order Which direction to order categories. Accepts 'ASC' or 'DESC'. |
|
496 * Default 'ASC'. |
|
497 * @type string $orderby The column to use for ordering categories. Default 'name'. |
|
498 * @type string $separator Separator between links. Default '<br />'. |
506 * @type string $separator Separator between links. Default '<br />'. |
499 * @type bool|int $show_count Whether to show how many posts are in the category. Default 0. |
507 * @type bool|int $show_count Whether to include post counts. Accepts 0, 1, or their bool equivalents. |
|
508 * Default 0. |
500 * @type string $show_option_all Text to display for showing all categories. Default empty string. |
509 * @type string $show_option_all Text to display for showing all categories. Default empty string. |
501 * @type string $show_option_none Text to display for the 'no categories' option. |
510 * @type string $show_option_none Text to display for the 'no categories' option. |
502 * Default 'No categories'. |
511 * Default 'No categories'. |
503 * @type string $style The style used to display the categories list. If 'list', categories |
512 * @type string $style The style used to display the categories list. If 'list', categories |
504 * will be output as an unordered list. If left empty or another value, |
513 * will be output as an unordered list. If left empty or another value, |
505 * categories will be output separated by `<br>` tags. Default 'list'. |
514 * categories will be output separated by `<br>` tags. Default 'list'. |
506 * @type string $taxonomy Taxonomy name. Default 'category'. |
|
507 * @type string $title_li Text to use for the list title `<li>` element. Pass an empty string |
515 * @type string $title_li Text to use for the list title `<li>` element. Pass an empty string |
508 * to disable. Default 'Categories'. |
516 * to disable. Default 'Categories'. |
509 * @type bool|int $use_desc_for_title Whether to use the category description as the title attribute. |
517 * @type bool|int $use_desc_for_title Whether to use the category description as the title attribute. |
510 * Default 1. |
518 * Accepts 0, 1, or their bool equivalents. Default 1. |
511 * } |
519 * } |
512 * @return false|string HTML content only if 'echo' argument is 0. |
520 * @return void|string|false Void if 'echo' argument is true, HTML list of categories if 'echo' is false. |
|
521 * False if the taxonomy does not exist. |
513 */ |
522 */ |
514 function wp_list_categories( $args = '' ) { |
523 function wp_list_categories( $args = '' ) { |
515 $defaults = array( |
524 $defaults = array( |
516 'child_of' => 0, |
525 'child_of' => 0, |
517 'current_category' => 0, |
526 'current_category' => 0, |
535 'taxonomy' => 'category', |
544 'taxonomy' => 'category', |
536 'title_li' => __( 'Categories' ), |
545 'title_li' => __( 'Categories' ), |
537 'use_desc_for_title' => 1, |
546 'use_desc_for_title' => 1, |
538 ); |
547 ); |
539 |
548 |
540 $r = wp_parse_args( $args, $defaults ); |
549 $parsed_args = wp_parse_args( $args, $defaults ); |
541 |
550 |
542 if ( ! isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) { |
551 if ( ! isset( $parsed_args['pad_counts'] ) && $parsed_args['show_count'] && $parsed_args['hierarchical'] ) { |
543 $r['pad_counts'] = true; |
552 $parsed_args['pad_counts'] = true; |
544 } |
553 } |
545 |
554 |
546 // Descendants of exclusions should be excluded too. |
555 // Descendants of exclusions should be excluded too. |
547 if ( true == $r['hierarchical'] ) { |
556 if ( true == $parsed_args['hierarchical'] ) { |
548 $exclude_tree = array(); |
557 $exclude_tree = array(); |
549 |
558 |
550 if ( $r['exclude_tree'] ) { |
559 if ( $parsed_args['exclude_tree'] ) { |
551 $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $r['exclude_tree'] ) ); |
560 $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $parsed_args['exclude_tree'] ) ); |
552 } |
561 } |
553 |
562 |
554 if ( $r['exclude'] ) { |
563 if ( $parsed_args['exclude'] ) { |
555 $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $r['exclude'] ) ); |
564 $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $parsed_args['exclude'] ) ); |
556 } |
565 } |
557 |
566 |
558 $r['exclude_tree'] = $exclude_tree; |
567 $parsed_args['exclude_tree'] = $exclude_tree; |
559 $r['exclude'] = ''; |
568 $parsed_args['exclude'] = ''; |
560 } |
569 } |
561 |
570 |
562 if ( ! isset( $r['class'] ) ) { |
571 if ( ! isset( $parsed_args['class'] ) ) { |
563 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy']; |
572 $parsed_args['class'] = ( 'category' === $parsed_args['taxonomy'] ) ? 'categories' : $parsed_args['taxonomy']; |
564 } |
573 } |
565 |
574 |
566 if ( ! taxonomy_exists( $r['taxonomy'] ) ) { |
575 if ( ! taxonomy_exists( $parsed_args['taxonomy'] ) ) { |
567 return false; |
576 return false; |
568 } |
577 } |
569 |
578 |
570 $show_option_all = $r['show_option_all']; |
579 $show_option_all = $parsed_args['show_option_all']; |
571 $show_option_none = $r['show_option_none']; |
580 $show_option_none = $parsed_args['show_option_none']; |
572 |
581 |
573 $categories = get_categories( $r ); |
582 $categories = get_categories( $parsed_args ); |
574 |
583 |
575 $output = ''; |
584 $output = ''; |
576 if ( $r['title_li'] && 'list' == $r['style'] && ( ! empty( $categories ) || ! $r['hide_title_if_empty'] ) ) { |
585 |
577 $output = '<li class="' . esc_attr( $r['class'] ) . '">' . $r['title_li'] . '<ul>'; |
586 if ( $parsed_args['title_li'] && 'list' === $parsed_args['style'] |
578 } |
587 && ( ! empty( $categories ) || ! $parsed_args['hide_title_if_empty'] ) |
|
588 ) { |
|
589 $output = '<li class="' . esc_attr( $parsed_args['class'] ) . '">' . $parsed_args['title_li'] . '<ul>'; |
|
590 } |
|
591 |
579 if ( empty( $categories ) ) { |
592 if ( empty( $categories ) ) { |
580 if ( ! empty( $show_option_none ) ) { |
593 if ( ! empty( $show_option_none ) ) { |
581 if ( 'list' == $r['style'] ) { |
594 if ( 'list' === $parsed_args['style'] ) { |
582 $output .= '<li class="cat-item-none">' . $show_option_none . '</li>'; |
595 $output .= '<li class="cat-item-none">' . $show_option_none . '</li>'; |
583 } else { |
596 } else { |
584 $output .= $show_option_none; |
597 $output .= $show_option_none; |
585 } |
598 } |
586 } |
599 } |
603 } |
616 } |
604 } |
617 } |
605 |
618 |
606 // Fallback for the 'All' link is the posts page. |
619 // Fallback for the 'All' link is the posts page. |
607 if ( ! $posts_page ) { |
620 if ( ! $posts_page ) { |
608 if ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) { |
621 if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) { |
609 $posts_page = get_permalink( get_option( 'page_for_posts' ) ); |
622 $posts_page = get_permalink( get_option( 'page_for_posts' ) ); |
610 } else { |
623 } else { |
611 $posts_page = home_url( '/' ); |
624 $posts_page = home_url( '/' ); |
612 } |
625 } |
613 } |
626 } |
614 |
627 |
615 $posts_page = esc_url( $posts_page ); |
628 $posts_page = esc_url( $posts_page ); |
616 if ( 'list' == $r['style'] ) { |
629 if ( 'list' === $parsed_args['style'] ) { |
617 $output .= "<li class='cat-item-all'><a href='$posts_page'>$show_option_all</a></li>"; |
630 $output .= "<li class='cat-item-all'><a href='$posts_page'>$show_option_all</a></li>"; |
618 } else { |
631 } else { |
619 $output .= "<a href='$posts_page'>$show_option_all</a>"; |
632 $output .= "<a href='$posts_page'>$show_option_all</a>"; |
620 } |
633 } |
621 } |
634 } |
622 |
635 |
623 if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) { |
636 if ( empty( $parsed_args['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) { |
624 $current_term_object = get_queried_object(); |
637 $current_term_object = get_queried_object(); |
625 if ( $current_term_object && $r['taxonomy'] === $current_term_object->taxonomy ) { |
638 if ( $current_term_object && $parsed_args['taxonomy'] === $current_term_object->taxonomy ) { |
626 $r['current_category'] = get_queried_object_id(); |
639 $parsed_args['current_category'] = get_queried_object_id(); |
627 } |
640 } |
628 } |
641 } |
629 |
642 |
630 if ( $r['hierarchical'] ) { |
643 if ( $parsed_args['hierarchical'] ) { |
631 $depth = $r['depth']; |
644 $depth = $parsed_args['depth']; |
632 } else { |
645 } else { |
633 $depth = -1; // Flat. |
646 $depth = -1; // Flat. |
634 } |
647 } |
635 $output .= walk_category_tree( $categories, $depth, $r ); |
648 $output .= walk_category_tree( $categories, $depth, $parsed_args ); |
636 } |
649 } |
637 |
650 |
638 if ( $r['title_li'] && 'list' == $r['style'] && ( ! empty( $categories ) || ! $r['hide_title_if_empty'] ) ) { |
651 if ( $parsed_args['title_li'] && 'list' === $parsed_args['style'] |
|
652 && ( ! empty( $categories ) || ! $parsed_args['hide_title_if_empty'] ) |
|
653 ) { |
639 $output .= '</ul></li>'; |
654 $output .= '</ul></li>'; |
640 } |
655 } |
641 |
656 |
642 /** |
657 /** |
643 * Filters the HTML output of a taxonomy list. |
658 * Filters the HTML output of a taxonomy list. |
694 'taxonomy' => 'post_tag', |
713 'taxonomy' => 'post_tag', |
695 'post_type' => '', |
714 'post_type' => '', |
696 'echo' => true, |
715 'echo' => true, |
697 'show_count' => 0, |
716 'show_count' => 0, |
698 ); |
717 ); |
699 $args = wp_parse_args( $args, $defaults ); |
718 |
|
719 $args = wp_parse_args( $args, $defaults ); |
700 |
720 |
701 $tags = get_terms( |
721 $tags = get_terms( |
702 $args['taxonomy'], |
|
703 array_merge( |
722 array_merge( |
704 $args, |
723 $args, |
705 array( |
724 array( |
706 'orderby' => 'count', |
725 'orderby' => 'count', |
707 'order' => 'DESC', |
726 'order' => 'DESC', |
708 ) |
727 ) |
709 ) |
728 ) |
710 ); // Always query top tags |
729 ); // Always query top tags. |
711 |
730 |
712 if ( empty( $tags ) || is_wp_error( $tags ) ) { |
731 if ( empty( $tags ) || is_wp_error( $tags ) ) { |
713 return; |
732 return; |
714 } |
733 } |
715 |
734 |
716 foreach ( $tags as $key => $tag ) { |
735 foreach ( $tags as $key => $tag ) { |
717 if ( 'edit' == $args['link'] ) { |
736 if ( 'edit' === $args['link'] ) { |
718 $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] ); |
737 $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] ); |
719 } else { |
738 } else { |
720 $link = get_term_link( intval( $tag->term_id ), $tag->taxonomy ); |
739 $link = get_term_link( intval( $tag->term_id ), $tag->taxonomy ); |
721 } |
740 } |
|
741 |
722 if ( is_wp_error( $link ) ) { |
742 if ( is_wp_error( $link ) ) { |
723 return; |
743 return; |
724 } |
744 } |
725 |
745 |
726 $tags[ $key ]->link = $link; |
746 $tags[ $key ]->link = $link; |
727 $tags[ $key ]->id = $tag->term_id; |
747 $tags[ $key ]->id = $tag->term_id; |
728 } |
748 } |
729 |
749 |
730 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args |
750 // Here's where those top tags get sorted according to $args. |
|
751 $return = wp_generate_tag_cloud( $tags, $args ); |
731 |
752 |
732 /** |
753 /** |
733 * Filters the tag cloud output. |
754 * Filters the tag cloud output. |
734 * |
755 * |
735 * @since 2.3.0 |
756 * @since 2.3.0 |
736 * |
757 * |
737 * @param string $return HTML output of the tag cloud. |
758 * @param string|array $return Tag cloud as a string or an array, depending on 'format' argument. |
738 * @param array $args An array of tag cloud arguments. |
759 * @param array $args An array of tag cloud arguments. |
739 */ |
760 */ |
740 $return = apply_filters( 'wp_tag_cloud', $return, $args ); |
761 $return = apply_filters( 'wp_tag_cloud', $return, $args ); |
741 |
762 |
742 if ( 'array' == $args['format'] || empty( $args['echo'] ) ) { |
763 if ( 'array' === $args['format'] || empty( $args['echo'] ) ) { |
743 return $return; |
764 return $return; |
744 } |
765 } |
745 |
766 |
746 echo $return; |
767 echo $return; |
747 } |
768 } |
833 if ( isset( $args['topic_count_text'] ) ) { |
854 if ( isset( $args['topic_count_text'] ) ) { |
834 // First look for nooped plural support via topic_count_text. |
855 // First look for nooped plural support via topic_count_text. |
835 $translate_nooped_plural = $args['topic_count_text']; |
856 $translate_nooped_plural = $args['topic_count_text']; |
836 } elseif ( ! empty( $args['topic_count_text_callback'] ) ) { |
857 } elseif ( ! empty( $args['topic_count_text_callback'] ) ) { |
837 // Look for the alternative callback style. Ignore the previous default. |
858 // Look for the alternative callback style. Ignore the previous default. |
838 if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) { |
859 if ( 'default_topic_count_text' === $args['topic_count_text_callback'] ) { |
|
860 /* translators: %s: Number of items (tags). */ |
839 $translate_nooped_plural = _n_noop( '%s item', '%s items' ); |
861 $translate_nooped_plural = _n_noop( '%s item', '%s items' ); |
840 } else { |
862 } else { |
841 $translate_nooped_plural = false; |
863 $translate_nooped_plural = false; |
842 } |
864 } |
843 } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) { |
865 } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) { |
844 // If no callback exists, look for the old-style single_text and multiple_text arguments. |
866 // If no callback exists, look for the old-style single_text and multiple_text arguments. |
845 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural |
867 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural |
846 $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] ); |
868 $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] ); |
847 } else { |
869 } else { |
848 // This is the default for when no callback, plural, or argument is passed in. |
870 // This is the default for when no callback, plural, or argument is passed in. |
|
871 /* translators: %s: Number of items (tags). */ |
849 $translate_nooped_plural = _n_noop( '%s item', '%s items' ); |
872 $translate_nooped_plural = _n_noop( '%s item', '%s items' ); |
850 } |
873 } |
851 |
874 |
852 /** |
875 /** |
853 * Filters how the items in a tag cloud are sorted. |
876 * Filters how the items in a tag cloud are sorted. |
1049 function _wp_object_count_sort_cb( $a, $b ) { |
1072 function _wp_object_count_sort_cb( $a, $b ) { |
1050 return ( $a->count > $b->count ); |
1073 return ( $a->count > $b->count ); |
1051 } |
1074 } |
1052 |
1075 |
1053 // |
1076 // |
1054 // Helper functions |
1077 // Helper functions. |
1055 // |
1078 // |
1056 |
1079 |
1057 /** |
1080 /** |
1058 * Retrieve HTML list content for category list. |
1081 * Retrieves HTML list content for category list. |
|
1082 * |
|
1083 * @since 2.1.0 |
|
1084 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it |
|
1085 * to the function signature. |
1059 * |
1086 * |
1060 * @uses Walker_Category to create HTML list content. |
1087 * @uses Walker_Category to create HTML list content. |
1061 * @since 2.1.0 |
1088 * @see Walker::walk() for parameters and return description. |
1062 * @see Walker_Category::walk() for parameters and return description. |
1089 * |
|
1090 * @param mixed ...$args Elements array, maximum hierarchical depth and optional additional arguments. |
1063 * @return string |
1091 * @return string |
1064 */ |
1092 */ |
1065 function walk_category_tree() { |
1093 function walk_category_tree( ...$args ) { |
1066 $args = func_get_args(); |
1094 // The user's options are the third parameter. |
1067 // the user's options are the third parameter |
|
1068 if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) { |
1095 if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) { |
1069 $walker = new Walker_Category; |
1096 $walker = new Walker_Category; |
1070 } else { |
1097 } else { |
|
1098 /** |
|
1099 * @var Walker $walker |
|
1100 */ |
1071 $walker = $args[2]['walker']; |
1101 $walker = $args[2]['walker']; |
1072 } |
1102 } |
1073 return call_user_func_array( array( $walker, 'walk' ), $args ); |
1103 return $walker->walk( ...$args ); |
1074 } |
1104 } |
1075 |
1105 |
1076 /** |
1106 /** |
1077 * Retrieve HTML dropdown (select) content for category list. |
1107 * Retrieves HTML dropdown (select) content for category list. |
|
1108 * |
|
1109 * @since 2.1.0 |
|
1110 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it |
|
1111 * to the function signature. |
1078 * |
1112 * |
1079 * @uses Walker_CategoryDropdown to create HTML dropdown content. |
1113 * @uses Walker_CategoryDropdown to create HTML dropdown content. |
1080 * @since 2.1.0 |
1114 * @see Walker::walk() for parameters and return description. |
1081 * @see Walker_CategoryDropdown::walk() for parameters and return description. |
1115 * |
|
1116 * @param mixed ...$args Elements array, maximum hierarchical depth and optional additional arguments. |
1082 * @return string |
1117 * @return string |
1083 */ |
1118 */ |
1084 function walk_category_dropdown_tree() { |
1119 function walk_category_dropdown_tree( ...$args ) { |
1085 $args = func_get_args(); |
1120 // The user's options are the third parameter. |
1086 // the user's options are the third parameter |
|
1087 if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) { |
1121 if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) { |
1088 $walker = new Walker_CategoryDropdown; |
1122 $walker = new Walker_CategoryDropdown; |
1089 } else { |
1123 } else { |
|
1124 /** |
|
1125 * @var Walker $walker |
|
1126 */ |
1090 $walker = $args[2]['walker']; |
1127 $walker = $args[2]['walker']; |
1091 } |
1128 } |
1092 return call_user_func_array( array( $walker, 'walk' ), $args ); |
1129 return $walker->walk( ...$args ); |
1093 } |
1130 } |
1094 |
1131 |
1095 // |
1132 // |
1096 // Tags |
1133 // Tags. |
1097 // |
1134 // |
1098 |
1135 |
1099 /** |
1136 /** |
1100 * Retrieve the link to the tag. |
1137 * Retrieves the link to the tag. |
1101 * |
1138 * |
1102 * @since 2.3.0 |
1139 * @since 2.3.0 |
|
1140 * |
1103 * @see get_term_link() |
1141 * @see get_term_link() |
1104 * |
1142 * |
1105 * @param int|object $tag Tag ID or object. |
1143 * @param int|object $tag Tag ID or object. |
1106 * @return string Link on success, empty string if tag does not exist. |
1144 * @return string Link on success, empty string if tag does not exist. |
1107 */ |
1145 */ |
1108 function get_tag_link( $tag ) { |
1146 function get_tag_link( $tag ) { |
1109 return get_category_link( $tag ); |
1147 return get_category_link( $tag ); |
1110 } |
1148 } |
1111 |
1149 |
1112 /** |
1150 /** |
1113 * Retrieve the tags for a post. |
1151 * Retrieves the tags for a post. |
1114 * |
1152 * |
1115 * @since 2.3.0 |
1153 * @since 2.3.0 |
1116 * |
1154 * |
1117 * @param int $id Post ID. |
1155 * @param int $post_id Post ID. |
1118 * @return array|false|WP_Error Array of tag objects on success, false on failure. |
1156 * @return array|false|WP_Error Array of tag objects on success, false on failure. |
1119 */ |
1157 */ |
1120 function get_the_tags( $id = 0 ) { |
1158 function get_the_tags( $post_id = 0 ) { |
|
1159 $terms = get_the_terms( $post_id, 'post_tag' ); |
1121 |
1160 |
1122 /** |
1161 /** |
1123 * Filters the array of tags for the given post. |
1162 * Filters the array of tags for the given post. |
1124 * |
1163 * |
1125 * @since 2.3.0 |
1164 * @since 2.3.0 |
1126 * |
1165 * |
1127 * @see get_the_terms() |
1166 * @see get_the_terms() |
1128 * |
1167 * |
1129 * @param WP_Term[] $terms An array of tags for the given post. |
1168 * @param WP_Term[] $terms An array of tags for the given post. |
1130 */ |
1169 */ |
1131 return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) ); |
1170 return apply_filters( 'get_the_tags', $terms ); |
1132 } |
1171 } |
1133 |
1172 |
1134 /** |
1173 /** |
1135 * Retrieve the tags for a post formatted as a string. |
1174 * Retrieves the tags for a post formatted as a string. |
1136 * |
1175 * |
1137 * @since 2.3.0 |
1176 * @since 2.3.0 |
1138 * |
1177 * |
1139 * @param string $before Optional. Before tags. |
1178 * @param string $before Optional. String to use before the tags. Default empty. |
1140 * @param string $sep Optional. Between tags. |
1179 * @param string $sep Optional. String to use between the tags. Default empty. |
1141 * @param string $after Optional. After tags. |
1180 * @param string $after Optional. String to use after the tags. Default empty. |
1142 * @param int $id Optional. Post ID. Defaults to the current post. |
1181 * @param int $post_id Optional. Post ID. Defaults to the current post ID. |
1143 * @return string|false|WP_Error A list of tags on success, false if there are no terms, WP_Error on failure. |
1182 * @return string|false|WP_Error A list of tags on success, false if there are no terms, |
1144 */ |
1183 * WP_Error on failure. |
1145 function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) { |
1184 */ |
|
1185 function get_the_tag_list( $before = '', $sep = '', $after = '', $post_id = 0 ) { |
|
1186 $tag_list = get_the_term_list( $post_id, 'post_tag', $before, $sep, $after ); |
1146 |
1187 |
1147 /** |
1188 /** |
1148 * Filters the tags list for a given post. |
1189 * Filters the tags list for a given post. |
1149 * |
1190 * |
1150 * @since 2.3.0 |
1191 * @since 2.3.0 |
1151 * |
1192 * |
1152 * @param string $tag_list List of tags. |
1193 * @param string $tag_list List of tags. |
1153 * @param string $before String to use before tags. |
1194 * @param string $before String to use before the tags. |
1154 * @param string $sep String to use between the tags. |
1195 * @param string $sep String to use between the tags. |
1155 * @param string $after String to use after tags. |
1196 * @param string $after String to use after the tags. |
1156 * @param int $id Post ID. |
1197 * @param int $post_id Post ID. |
1157 */ |
1198 */ |
1158 return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id ); |
1199 return apply_filters( 'the_tags', $tag_list, $before, $sep, $after, $post_id ); |
1159 } |
1200 } |
1160 |
1201 |
1161 /** |
1202 /** |
1162 * Retrieve the tags for a post. |
1203 * Displays the tags for a post. |
1163 * |
1204 * |
1164 * @since 2.3.0 |
1205 * @since 2.3.0 |
1165 * |
1206 * |
1166 * @param string $before Optional. Before list. |
1207 * @param string $before Optional. String to use before the tags. Defaults to 'Tags:'. |
1167 * @param string $sep Optional. Separate items using this. |
1208 * @param string $sep Optional. String to use between the tags. Default ', '. |
1168 * @param string $after Optional. After list. |
1209 * @param string $after Optional. String to use after the tags. Default empty. |
1169 */ |
1210 */ |
1170 function the_tags( $before = null, $sep = ', ', $after = '' ) { |
1211 function the_tags( $before = null, $sep = ', ', $after = '' ) { |
1171 if ( null === $before ) { |
1212 if ( null === $before ) { |
1172 $before = __( 'Tags: ' ); |
1213 $before = __( 'Tags: ' ); |
1173 } |
1214 } |
1178 echo $the_tags; |
1219 echo $the_tags; |
1179 } |
1220 } |
1180 } |
1221 } |
1181 |
1222 |
1182 /** |
1223 /** |
1183 * Retrieve tag description. |
1224 * Retrieves tag description. |
1184 * |
1225 * |
1185 * @since 2.8.0 |
1226 * @since 2.8.0 |
1186 * |
1227 * |
1187 * @param int $tag Optional. Tag ID. Will use global tag ID by default. |
1228 * @param int $tag Optional. Tag ID. Defaults to the current tag ID. |
1188 * @return string Tag description, available. |
1229 * @return string Tag description, if available. |
1189 */ |
1230 */ |
1190 function tag_description( $tag = 0 ) { |
1231 function tag_description( $tag = 0 ) { |
1191 return term_description( $tag ); |
1232 return term_description( $tag ); |
1192 } |
1233 } |
1193 |
1234 |
1194 /** |
1235 /** |
1195 * Retrieve term description. |
1236 * Retrieves term description. |
1196 * |
1237 * |
1197 * @since 2.8.0 |
1238 * @since 2.8.0 |
1198 * @since 4.9.2 The `$taxonomy` parameter was deprecated. |
1239 * @since 4.9.2 The `$taxonomy` parameter was deprecated. |
1199 * |
1240 * |
1200 * @param int $term Optional. Term ID. Will use global term ID by default. |
1241 * @param int $term Optional. Term ID. Defaults to the current term ID. |
1201 * @param null $deprecated Deprecated argument. |
1242 * @param null $deprecated Deprecated. Not used. |
1202 * @return string Term description, available. |
1243 * @return string Term description, if available. |
1203 */ |
1244 */ |
1204 function term_description( $term = 0, $deprecated = null ) { |
1245 function term_description( $term = 0, $deprecated = null ) { |
1205 if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) { |
1246 if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) { |
1206 $term = get_queried_object(); |
1247 $term = get_queried_object(); |
1207 if ( $term ) { |
1248 if ( $term ) { |
1208 $term = $term->term_id; |
1249 $term = $term->term_id; |
1209 } |
1250 } |
1210 } |
1251 } |
|
1252 |
1211 $description = get_term_field( 'description', $term ); |
1253 $description = get_term_field( 'description', $term ); |
|
1254 |
1212 return is_wp_error( $description ) ? '' : $description; |
1255 return is_wp_error( $description ) ? '' : $description; |
1213 } |
1256 } |
1214 |
1257 |
1215 /** |
1258 /** |
1216 * Retrieve the terms of the taxonomy that are attached to the post. |
1259 * Retrieves the terms of the taxonomy that are attached to the post. |
1217 * |
1260 * |
1218 * @since 2.5.0 |
1261 * @since 2.5.0 |
1219 * |
1262 * |
1220 * @param int|WP_Post $post Post ID or object. |
1263 * @param int|WP_Post $post Post ID or object. |
1221 * @param string $taxonomy Taxonomy name. |
1264 * @param string $taxonomy Taxonomy name. |
1222 * @return WP_Term[]|false|WP_Error Array of WP_Term objects on success, false if there are no terms |
1265 * @return WP_Term[]|false|WP_Error Array of WP_Term objects on success, false if there are no terms |
1223 * or the post does not exist, WP_Error on failure. |
1266 * or the post does not exist, WP_Error on failure. |
1224 */ |
1267 */ |
1225 function get_the_terms( $post, $taxonomy ) { |
1268 function get_the_terms( $post, $taxonomy ) { |
1226 if ( ! $post = get_post( $post ) ) { |
1269 $post = get_post( $post ); |
|
1270 if ( ! $post ) { |
1227 return false; |
1271 return false; |
1228 } |
1272 } |
1229 |
1273 |
1230 $terms = get_object_term_cache( $post->ID, $taxonomy ); |
1274 $terms = get_object_term_cache( $post->ID, $taxonomy ); |
1231 if ( false === $terms ) { |
1275 if ( false === $terms ) { |
1399 */ |
1446 */ |
1400 echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after ); |
1447 echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after ); |
1401 } |
1448 } |
1402 |
1449 |
1403 /** |
1450 /** |
1404 * Check if the current post has any of given category. |
1451 * Checks if the current post has any of given category. |
|
1452 * |
|
1453 * The given categories are checked against the post's categories' term_ids, names and slugs. |
|
1454 * Categories given as integers will only be checked against the post's categories' term_ids. |
|
1455 * |
|
1456 * If no categories are given, determines if post has any categories. |
1405 * |
1457 * |
1406 * @since 3.1.0 |
1458 * @since 3.1.0 |
1407 * |
1459 * |
1408 * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for. |
1460 * @param string|int|array $category Optional. The category name/term_id/slug, |
1409 * @param int|object $post Optional. Post to check instead of the current post. |
1461 * or an array of them to check for. Default empty. |
1410 * @return bool True if the current post has any of the given categories (or any category, if no category specified). |
1462 * @param int|object $post Optional. Post to check instead of the current post. |
|
1463 * @return bool True if the current post has any of the given categories |
|
1464 * (or any category, if no category specified). False otherwise. |
1411 */ |
1465 */ |
1412 function has_category( $category = '', $post = null ) { |
1466 function has_category( $category = '', $post = null ) { |
1413 return has_term( $category, 'category', $post ); |
1467 return has_term( $category, 'category', $post ); |
1414 } |
1468 } |
1415 |
1469 |
1416 /** |
1470 /** |
1417 * Checks if the current post has any of given tags. |
1471 * Checks if the current post has any of given tags. |
1418 * |
1472 * |
1419 * The given tags are checked against the post's tags' term_ids, names and slugs. |
1473 * The given tags are checked against the post's tags' term_ids, names and slugs. |
1420 * Tags given as integers will only be checked against the post's tags' term_ids. |
1474 * Tags given as integers will only be checked against the post's tags' term_ids. |
|
1475 * |
1421 * If no tags are given, determines if post has any tags. |
1476 * If no tags are given, determines if post has any tags. |
1422 * |
|
1423 * Prior to v2.7 of WordPress, tags given as integers would also be checked against the post's tags' names and slugs (in addition to term_ids) |
|
1424 * Prior to v2.7, this function could only be used in the WordPress Loop. |
|
1425 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object. |
|
1426 * |
1477 * |
1427 * For more information on this and similar theme functions, check out |
1478 * For more information on this and similar theme functions, check out |
1428 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
1479 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
1429 * Conditional Tags} article in the Theme Developer Handbook. |
1480 * Conditional Tags} article in the Theme Developer Handbook. |
1430 * |
1481 * |
1431 * @since 2.6.0 |
1482 * @since 2.6.0 |
1432 * |
1483 * @since 2.7.0 Tags given as integers are only checked against |
1433 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for. |
1484 * the post's tags' term_ids, not names or slugs. |
1434 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0) |
1485 * @since 2.7.0 Can be used outside of the WordPress Loop if `$post` is provided. |
1435 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified). |
1486 * |
|
1487 * @param string|int|array $tag Optional. The tag name/term_id/slug, |
|
1488 * or an array of them to check for. Default empty. |
|
1489 * @param int|object $post Optional. Post to check instead of the current post. |
|
1490 * @return bool True if the current post has any of the given tags |
|
1491 * (or any tag, if no tag specified). False otherwise. |
1436 */ |
1492 */ |
1437 function has_tag( $tag = '', $post = null ) { |
1493 function has_tag( $tag = '', $post = null ) { |
1438 return has_term( $tag, 'post_tag', $post ); |
1494 return has_term( $tag, 'post_tag', $post ); |
1439 } |
1495 } |
1440 |
1496 |
1441 /** |
1497 /** |
1442 * Check if the current post has any of given terms. |
1498 * Checks if the current post has any of given terms. |
1443 * |
1499 * |
1444 * The given terms are checked against the post's terms' term_ids, names and slugs. |
1500 * The given terms are checked against the post's terms' term_ids, names and slugs. |
1445 * Terms given as integers will only be checked against the post's terms' term_ids. |
1501 * Terms given as integers will only be checked against the post's terms' term_ids. |
|
1502 * |
1446 * If no terms are given, determines if post has any terms. |
1503 * If no terms are given, determines if post has any terms. |
1447 * |
1504 * |
1448 * @since 3.1.0 |
1505 * @since 3.1.0 |
1449 * |
1506 * |
1450 * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for. |
1507 * @param string|int|array $term Optional. The term name/term_id/slug, |
1451 * @param string $taxonomy Taxonomy name |
1508 * or an array of them to check for. Default empty. |
1452 * @param int|object $post Optional. Post to check instead of the current post. |
1509 * @param string $taxonomy Optional. Taxonomy name. Default empty. |
1453 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified). |
1510 * @param int|WP_Post $post Optional. Post to check instead of the current post. |
|
1511 * @return bool True if the current post has any of the given terms |
|
1512 * (or any term, if no term specified). False otherwise. |
1454 */ |
1513 */ |
1455 function has_term( $term = '', $taxonomy = '', $post = null ) { |
1514 function has_term( $term = '', $taxonomy = '', $post = null ) { |
1456 $post = get_post( $post ); |
1515 $post = get_post( $post ); |
1457 |
1516 |
1458 if ( ! $post ) { |
1517 if ( ! $post ) { |