| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Taxonomy API: Core category-specific template tags 4 * 5 * @package WordPress 6 * @subpackage Template 7 * @since 1.2.0 8 */ 9 10 /** 11 * Retrieves category link URL. 12 * 13 * @since 1.0.0 14 * 15 * @see get_term_link() 16 * 17 * @param int|object $category Category ID or object. 18 * @return string Link on success, empty string if category does not exist. 19 */ 20 function get_category_link( $category ) { 21 if ( ! is_object( $category ) ) { 22 $category = (int) $category; 23 } 24 25 $category = get_term_link( $category ); 26 27 if ( is_wp_error( $category ) ) { 28 return ''; 29 } 30 31 return $category; 32 } 33 34 /** 35 * Retrieves category parents with separator. 36 * 37 * @since 1.2.0 38 * @since 4.8.0 The `$visited` parameter was deprecated and renamed to `$deprecated`. 39 * 40 * @param int $category_id Category ID. 41 * @param bool $link Optional. Whether to format with link. Default false. 42 * @param string $separator Optional. How to separate categories. Default '/'. 43 * @param bool $nicename Optional. Whether to use nice name for display. Default false. 44 * @param array $deprecated Not used. 45 * @return string|WP_Error A list of category parents on success, WP_Error on failure. 46 * 47 * @phpstan-param array{} $deprecated 48 */ 49 function get_category_parents( $category_id, $link = false, $separator = '/', $nicename = false, $deprecated = array() ) { 50 51 if ( ! empty( $deprecated ) ) { 52 _deprecated_argument( __FUNCTION__, '4.8.0' ); 53 } 54 55 $format = $nicename ? 'slug' : 'name'; 56 57 $args = array( 58 'separator' => $separator, 59 'link' => $link, 60 'format' => $format, 61 ); 62 63 return get_term_parents_list( $category_id, 'category', $args ); 64 } 65 66 /** 67 * Retrieves post categories. 68 * 69 * This tag may be used outside The Loop by passing a post ID as the parameter. 70 * 71 * Note: This function only returns results from the default "category" taxonomy. 72 * For custom taxonomies use get_the_terms(). 73 * 74 * @since 0.71 75 * 76 * @param int|false $post_id Optional. The post ID. Defaults to current post ID. 77 * @return WP_Term[] Array of WP_Term objects, one for each category assigned to the post. 78 */ 79 function get_the_category( $post_id = false ) { 80 $categories = get_the_terms( $post_id, 'category' ); 81 if ( ! $categories || is_wp_error( $categories ) ) { 82 $categories = array(); 83 } 84 85 $categories = array_values( $categories ); 86 87 foreach ( array_keys( $categories ) as $key ) { 88 _make_cat_compat( $categories[ $key ] ); 89 } 90 91 /** 92 * Filters the array of categories to return for a post. 93 * 94 * @since 3.1.0 95 * @since 4.4.0 Added the `$post_id` parameter. 96 * 97 * @param WP_Term[] $categories An array of categories to return for the post. 98 * @param int|false $post_id The post ID. 99 */ 100 return apply_filters( 'get_the_categories', $categories, $post_id ); 101 } 102 103 /** 104 * Retrieves category name based on category ID. 105 * 106 * @since 0.71 107 * 108 * @param int $cat_id Category ID. 109 * @return string|WP_Error Category name on success, WP_Error on failure. 110 */ 111 function get_the_category_by_ID( $cat_id ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid 112 $cat_id = (int) $cat_id; 113 $category = get_term( $cat_id ); 114 115 if ( is_wp_error( $category ) ) { 116 return $category; 117 } 118 119 return ( $category ) ? $category->name : ''; 120 } 121 122 /** 123 * Retrieves category list for a post in either HTML list or custom format. 124 * 125 * Generally used for quick, delimited (e.g. comma-separated) lists of categories, 126 * as part of a post entry meta. 127 * 128 * For a more powerful, list-based function, see wp_list_categories(). 129 * 130 * @since 1.5.1 131 * 132 * @see wp_list_categories() 133 * 134 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 135 * 136 * @param string $separator Optional. Separator between the categories. By default, the links are placed 137 * in an unordered list. An empty string will result in the default behavior. 138 * @param string $parents Optional. How to display the parents. Accepts 'multiple', 'single', or empty. 139 * Default empty string. 140 * @param int|false $post_id Optional. ID of the post to retrieve categories for. Defaults to the current post. 141 * @return string Category list for a post. 142 */ 143 function get_the_category_list( $separator = '', $parents = '', $post_id = false ) { 144 global $wp_rewrite; 145 146 if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) ) { 147 /** This filter is documented in wp-includes/category-template.php */ 148 return apply_filters( 'the_category', '', $separator, $parents ); 149 } 150 151 /** 152 * Filters the categories before building the category list. 153 * 154 * @since 4.4.0 155 * 156 * @param WP_Term[] $categories An array of the post's categories. 157 * @param int|false $post_id ID of the post to retrieve categories for. 158 * When `false`, defaults to the current post in the loop. 159 */ 160 $categories = apply_filters( 'the_category_list', get_the_category( $post_id ), $post_id ); 161 162 if ( empty( $categories ) ) { 163 /** This filter is documented in wp-includes/category-template.php */ 164 return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents ); 165 } 166 167 $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"'; 168 169 $thelist = ''; 170 if ( '' === $separator ) { 171 $thelist .= '<ul class="post-categories">'; 172 foreach ( $categories as $category ) { 173 $thelist .= "\n\t<li>"; 174 switch ( strtolower( $parents ) ) { 175 case 'multiple': 176 if ( $category->parent ) { 177 $thelist .= get_category_parents( $category->parent, true, $separator ); 178 } 179 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name . '</a></li>'; 180 break; 181 case 'single': 182 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>'; 183 if ( $category->parent ) { 184 $thelist .= get_category_parents( $category->parent, false, $separator ); 185 } 186 $thelist .= $category->name . '</a></li>'; 187 break; 188 case '': 189 default: 190 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name . '</a></li>'; 191 } 192 } 193 $thelist .= '</ul>'; 194 } else { 195 $i = 0; 196 foreach ( $categories as $category ) { 197 if ( 0 < $i ) { 198 $thelist .= $separator; 199 } 200 switch ( strtolower( $parents ) ) { 201 case 'multiple': 202 if ( $category->parent ) { 203 $thelist .= get_category_parents( $category->parent, true, $separator ); 204 } 205 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name . '</a>'; 206 break; 207 case 'single': 208 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>'; 209 if ( $category->parent ) { 210 $thelist .= get_category_parents( $category->parent, false, $separator ); 211 } 212 $thelist .= "$category->name</a>"; 213 break; 214 case '': 215 default: 216 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name . '</a>'; 217 } 218 ++$i; 219 } 220 } 221 222 /** 223 * Filters the category or list of categories. 224 * 225 * @since 1.2.0 226 * 227 * @param string $thelist List of categories for the current post. 228 * @param string $separator Separator used between the categories. 229 * @param string $parents How to display the category parents. Accepts 'multiple', 230 * 'single', or empty. 231 */ 232 return apply_filters( 'the_category', $thelist, $separator, $parents ); 233 } 234 235 /** 236 * Checks if the current post is within any of the given categories. 237 * 238 * The given categories are checked against the post's categories' term_ids, names and slugs. 239 * Categories given as integers will only be checked against the post's categories' term_ids. 240 * 241 * Prior to v2.5 of WordPress, category names were not supported. 242 * Prior to v2.7, category slugs were not supported. 243 * Prior to v2.7, only one category could be compared: in_category( $single_category ). 244 * Prior to v2.7, this function could only be used in the WordPress Loop. 245 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object. 246 * 247 * For more information on this and similar theme functions, check out 248 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 249 * Conditional Tags} article in the Theme Developer Handbook. 250 * 251 * @since 1.2.0 252 * @since 2.7.0 The `$post` parameter was added. 253 * 254 * @param int|string|int[]|string[] $category Category ID, name, slug, or array of such 255 * to check against. 256 * @param int|null|WP_Post $post Optional. Post to check. Defaults to the current post. 257 * @return bool True if the current post is in any of the given categories. 258 */ 259 function in_category( $category, $post = null ) { 260 if ( empty( $category ) ) { 261 return false; 262 } 263 264 return has_category( $category, $post ); 265 } 266 267 /** 268 * Displays category list for a post in either HTML list or custom format. 269 * 270 * @since 0.71 271 * 272 * @param string $separator Optional. Separator between the categories. By default, the links are placed 273 * in an unordered list. An empty string will result in the default behavior. 274 * @param string $parents Optional. How to display the parents. Accepts 'multiple', 'single', or empty. 275 * Default empty string. 276 * @param int|false $post_id Optional. ID of the post to retrieve categories for. Defaults to the current post. 277 */ 278 function the_category( $separator = '', $parents = '', $post_id = false ) { 279 echo get_the_category_list( $separator, $parents, $post_id ); 280 } 281 282 /** 283 * Retrieves category description. 284 * 285 * @since 1.0.0 286 * 287 * @param int $category Optional. Category ID. Defaults to the current category ID. 288 * @return string Category description, if available. 289 */ 290 function category_description( $category = 0 ) { 291 return term_description( $category ); 292 } 293 294 /** 295 * Displays or retrieves the HTML dropdown list of categories. 296 * 297 * The 'hierarchical' argument, which is disabled by default, will override the 298 * depth argument, unless it is true. When the argument is false, it will 299 * display all of the categories. When it is enabled it will use the value in 300 * the 'depth' argument. 301 * 302 * @since 2.1.0 303 * @since 4.2.0 Introduced the `value_field` argument. 304 * @since 4.6.0 Introduced the `required` argument. 305 * @since 6.1.0 Introduced the `aria_describedby` argument. 306 * 307 * @param array|string $args { 308 * Optional. Array or string of arguments to generate a categories drop-down element. See WP_Term_Query::__construct() 309 * for information on additional accepted arguments. 310 * 311 * @type string $show_option_all Text to display for showing all categories. Default empty. 312 * @type string $show_option_none Text to display for showing no categories. Default empty. 313 * @type string $option_none_value Value to use when no category is selected. Default empty. 314 * @type string $orderby Which column to use for ordering categories. See get_terms() for a list 315 * of accepted values. Default 'id' (term_id). 316 * @type bool $pad_counts See get_terms() for an argument description. Default false. 317 * @type bool|int $show_count Whether to include post counts. Accepts 0, 1, or their bool equivalents. 318 * Default 0. 319 * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, or their 320 * bool equivalents. Default 1. 321 * @type bool|int $hierarchical Whether to traverse the taxonomy hierarchy. Accepts 0, 1, or their bool 322 * equivalents. Default 0. 323 * @type int $depth Maximum depth. Default 0. 324 * @type int $tab_index Tab index for the select element. Default 0 (no tabindex). 325 * @type string $name Value for the 'name' attribute of the select element. Default 'cat'. 326 * @type string $id Value for the 'id' attribute of the select element. Defaults to the value 327 * of `$name`. 328 * @type string $class Value for the 'class' attribute of the select element. Default 'postform'. 329 * @type int|string $selected Value of the option that should be selected. Default 0. 330 * @type string $value_field Term field that should be used to populate the 'value' attribute 331 * of the option elements. Accepts any valid term field: 'term_id', 'name', 332 * 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', 333 * 'parent', 'count'. Default 'term_id'. 334 * @type string|array $taxonomy Name of the taxonomy or taxonomies to retrieve. Default 'category'. 335 * @type bool $hide_if_empty True to skip generating markup if no categories are found. 336 * Default false (create select element even if no categories are found). 337 * @type bool $required Whether the `<select>` element should have the HTML5 'required' attribute. 338 * Default false. 339 * @type Walker $walker Walker object to use to build the output. Default empty which results in a 340 * Walker_CategoryDropdown instance being used. 341 * @type string $aria_describedby The 'id' of an element that contains descriptive text for the select. 342 * Default empty string. 343 * } 344 * @return string HTML dropdown list of categories. 345 */ 346 function wp_dropdown_categories( $args = '' ) { 347 $defaults = array( 348 'show_option_all' => '', 349 'show_option_none' => '', 350 'orderby' => 'id', 351 'order' => 'ASC', 352 'show_count' => 0, 353 'hide_empty' => 1, 354 'child_of' => 0, 355 'exclude' => '', 356 'echo' => 1, 357 'selected' => 0, 358 'hierarchical' => 0, 359 'name' => 'cat', 360 'id' => '', 361 'class' => 'postform', 362 'depth' => 0, 363 'tab_index' => 0, 364 'taxonomy' => 'category', 365 'hide_if_empty' => false, 366 'option_none_value' => -1, 367 'value_field' => 'term_id', 368 'required' => false, 369 'aria_describedby' => '', 370 ); 371 372 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0; 373 374 // Back compat. 375 if ( isset( $args['type'] ) && 'link' === $args['type'] ) { 376 _deprecated_argument( 377 __FUNCTION__, 378 '3.0.0', 379 sprintf( 380 /* translators: 1: "type => link", 2: "taxonomy => link_category" */ 381 __( '%1$s is deprecated. Use %2$s instead.' ), 382 '<code>type => link</code>', 383 '<code>taxonomy => link_category</code>' 384 ) 385 ); 386 $args['taxonomy'] = 'link_category'; 387 } 388 389 // Parse incoming $args into an array and merge it with $defaults. 390 $parsed_args = wp_parse_args( $args, $defaults ); 391 392 $option_none_value = $parsed_args['option_none_value']; 393 394 if ( ! isset( $parsed_args['pad_counts'] ) && $parsed_args['show_count'] && $parsed_args['hierarchical'] ) { 395 $parsed_args['pad_counts'] = true; 396 } 397 398 $tab_index = $parsed_args['tab_index']; 399 400 $tab_index_attribute = ''; 401 if ( (int) $tab_index > 0 ) { 402 $tab_index_attribute = " tabindex=\"$tab_index\""; 403 } 404 405 // Avoid clashes with the 'name' param of get_terms(). 406 $get_terms_args = $parsed_args; 407 unset( $get_terms_args['name'] ); 408 $categories = get_terms( $get_terms_args ); 409 410 $name = esc_attr( $parsed_args['name'] ); 411 $class = esc_attr( $parsed_args['class'] ); 412 $id = $parsed_args['id'] ? esc_attr( $parsed_args['id'] ) : $name; 413 $required = $parsed_args['required'] ? 'required' : ''; 414 415 $aria_describedby_attribute = $parsed_args['aria_describedby'] ? ' aria-describedby="' . esc_attr( $parsed_args['aria_describedby'] ) . '"' : ''; 416 417 if ( ! $parsed_args['hide_if_empty'] || ! empty( $categories ) ) { 418 $output = "<select $required name='$name' id='$id' class='$class'$tab_index_attribute$aria_describedby_attribute>\n"; 419 } else { 420 $output = ''; 421 } 422 if ( empty( $categories ) && ! $parsed_args['hide_if_empty'] && ! empty( $parsed_args['show_option_none'] ) ) { 423 424 /** 425 * Filters a taxonomy drop-down display element. 426 * 427 * A variety of taxonomy drop-down display elements can be modified 428 * just prior to display via this filter. Filterable arguments include 429 * 'show_option_none', 'show_option_all', and various forms of the 430 * term name. 431 * 432 * @since 1.2.0 433 * 434 * @see wp_dropdown_categories() 435 * 436 * @param string $element Category name. 437 * @param WP_Term|null $category The category object, or null if there's no corresponding category. 438 */ 439 $show_option_none = apply_filters( 'list_cats', $parsed_args['show_option_none'], null ); 440 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "' selected='selected'>$show_option_none</option>\n"; 441 } 442 443 if ( ! empty( $categories ) ) { 444 445 if ( $parsed_args['show_option_all'] ) { 446 447 /** This filter is documented in wp-includes/category-template.php */ 448 $show_option_all = apply_filters( 'list_cats', $parsed_args['show_option_all'], null ); 449 $selected = ( '0' === (string) $parsed_args['selected'] ) ? " selected='selected'" : ''; 450 $output .= "\t<option value='0'$selected>$show_option_all</option>\n"; 451 } 452 453 if ( $parsed_args['show_option_none'] ) { 454 455 /** This filter is documented in wp-includes/category-template.php */ 456 $show_option_none = apply_filters( 'list_cats', $parsed_args['show_option_none'], null ); 457 $selected = selected( $option_none_value, $parsed_args['selected'], false ); 458 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$selected>$show_option_none</option>\n"; 459 } 460 461 if ( $parsed_args['hierarchical'] ) { 462 $depth = $parsed_args['depth']; // Walk the full depth. 463 } else { 464 $depth = -1; // Flat. 465 } 466 $output .= walk_category_dropdown_tree( $categories, $depth, $parsed_args ); 467 } 468 469 if ( ! $parsed_args['hide_if_empty'] || ! empty( $categories ) ) { 470 $output .= "</select>\n"; 471 } 472 473 /** 474 * Filters the taxonomy drop-down output. 475 * 476 * @since 2.1.0 477 * 478 * @param string $output HTML output. 479 * @param array $parsed_args Arguments used to build the drop-down. 480 */ 481 $output = apply_filters( 'wp_dropdown_cats', $output, $parsed_args ); 482 483 if ( $parsed_args['echo'] ) { 484 echo $output; 485 } 486 487 return $output; 488 } 489 490 /** 491 * Displays or retrieves the HTML list of categories. 492 * 493 * @since 2.1.0 494 * @since 4.4.0 Introduced the `hide_title_if_empty` and `separator` arguments. 495 * @since 4.4.0 The `current_category` argument was modified to optionally accept an array of values. 496 * @since 6.1.0 Default value of the 'use_desc_for_title' argument was changed from 1 to 0. 497 * 498 * @param array|string $args { 499 * Array of optional arguments. See get_categories(), get_terms(), and WP_Term_Query::__construct() 500 * for information on additional accepted arguments. 501 * 502 * @type int|int[] $current_category ID of category, or array of IDs of categories, that should get the 503 * 'current-cat' class. Default 0. 504 * @type int $depth Category depth. Used for tab indentation. Default 0. 505 * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, or their 506 * bool equivalents. Default 1. 507 * @type int[]|string $exclude Array or comma/space-separated string of term IDs to exclude. 508 * If `$hierarchical` is true, descendants of `$exclude` terms will also 509 * be excluded; see `$exclude_tree`. See get_terms(). 510 * Default empty string. 511 * @type int[]|string $exclude_tree Array or comma/space-separated string of term IDs to exclude, along 512 * with their descendants. See get_terms(). Default empty string. 513 * @type string $feed Text to use for the feed link. Default 'Feed for all posts filed 514 * under [cat name]'. 515 * @type string $feed_image URL of an image to use for the feed link. Default empty string. 516 * @type string $feed_type Feed type. Used to build feed link. See get_term_feed_link(). 517 * Default empty string (default feed). 518 * @type bool $hide_title_if_empty Whether to hide the `$title_li` element if there are no terms in 519 * the list. Default false (title will always be shown). 520 * @type string $separator Separator between links. Default '<br />'. 521 * @type bool|int $show_count Whether to include post counts. Accepts 0, 1, or their bool equivalents. 522 * Default 0. 523 * @type string $show_option_all Text to display for showing all categories. Default empty string. 524 * @type string $show_option_none Text to display for the 'no categories' option. 525 * Default 'No categories'. 526 * @type string $style The style used to display the categories list. If 'list', categories 527 * will be output as an unordered list. If left empty or another value, 528 * categories will be output separated by `<br>` tags. Default 'list'. 529 * @type string $taxonomy Name of the taxonomy to retrieve. Default 'category'. 530 * @type string $title_li Text to use for the list title `<li>` element. Pass an empty string 531 * to disable. Default 'Categories'. 532 * @type bool|int $use_desc_for_title Whether to use the category description as the title attribute. 533 * Accepts 0, 1, or their bool equivalents. Default 0. 534 * @type Walker $walker Walker object to use to build the output. Default empty which results 535 * in a Walker_Category instance being used. 536 * } 537 * @return void|string|false Void if 'echo' argument is true, HTML list of categories if 'echo' is false. 538 * False if the taxonomy does not exist. 539 * 540 * @phpstan-return ( $args is array{ echo: false|0, ... } ? string|false : false|void ) 541 */ 542 function wp_list_categories( $args = '' ) { 543 $defaults = array( 544 'child_of' => 0, 545 'current_category' => 0, 546 'depth' => 0, 547 'echo' => 1, 548 'exclude' => '', 549 'exclude_tree' => '', 550 'feed' => '', 551 'feed_image' => '', 552 'feed_type' => '', 553 'hide_empty' => 1, 554 'hide_title_if_empty' => false, 555 'hierarchical' => true, 556 'order' => 'ASC', 557 'orderby' => 'name', 558 'separator' => '<br />', 559 'show_count' => 0, 560 'show_option_all' => '', 561 'show_option_none' => __( 'No categories' ), 562 'style' => 'list', 563 'taxonomy' => 'category', 564 'title_li' => __( 'Categories' ), 565 'use_desc_for_title' => 0, 566 ); 567 568 $parsed_args = wp_parse_args( $args, $defaults ); 569 570 if ( ! isset( $parsed_args['pad_counts'] ) && $parsed_args['show_count'] && $parsed_args['hierarchical'] ) { 571 $parsed_args['pad_counts'] = true; 572 } 573 574 // Descendants of exclusions should be excluded too. 575 if ( $parsed_args['hierarchical'] ) { 576 $exclude_tree = array(); 577 578 if ( $parsed_args['exclude_tree'] ) { 579 $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $parsed_args['exclude_tree'] ) ); 580 } 581 582 if ( $parsed_args['exclude'] ) { 583 $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $parsed_args['exclude'] ) ); 584 } 585 586 $parsed_args['exclude_tree'] = $exclude_tree; 587 $parsed_args['exclude'] = ''; 588 } 589 590 if ( ! isset( $parsed_args['class'] ) ) { 591 $parsed_args['class'] = ( 'category' === $parsed_args['taxonomy'] ) ? 'categories' : $parsed_args['taxonomy']; 592 } 593 594 if ( ! taxonomy_exists( $parsed_args['taxonomy'] ) ) { 595 return false; 596 } 597 598 $show_option_all = $parsed_args['show_option_all']; 599 $show_option_none = $parsed_args['show_option_none']; 600 601 $categories = get_categories( $parsed_args ); 602 603 $output = ''; 604 605 if ( $parsed_args['title_li'] && 'list' === $parsed_args['style'] 606 && ( ! empty( $categories ) || ! $parsed_args['hide_title_if_empty'] ) 607 ) { 608 $output = '<li class="' . esc_attr( $parsed_args['class'] ) . '">' . $parsed_args['title_li'] . '<ul>'; 609 } 610 611 if ( empty( $categories ) ) { 612 if ( ! empty( $show_option_none ) ) { 613 if ( 'list' === $parsed_args['style'] ) { 614 $output .= '<li class="cat-item-none">' . $show_option_none . '</li>'; 615 } else { 616 $output .= $show_option_none; 617 } 618 } 619 } else { 620 if ( ! empty( $show_option_all ) ) { 621 622 $posts_page = ''; 623 624 // For taxonomies that belong only to custom post types, point to a valid archive. 625 $taxonomy_object = get_taxonomy( $parsed_args['taxonomy'] ); 626 if ( ! in_array( 'post', $taxonomy_object->object_type, true ) && ! in_array( 'page', $taxonomy_object->object_type, true ) ) { 627 foreach ( $taxonomy_object->object_type as $object_type ) { 628 $_object_type = get_post_type_object( $object_type ); 629 630 // Grab the first one. 631 if ( ! empty( $_object_type->has_archive ) ) { 632 $posts_page = get_post_type_archive_link( $object_type ); 633 break; 634 } 635 } 636 } 637 638 // Fallback for the 'All' link is the posts page. 639 if ( ! $posts_page ) { 640 if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) { 641 $posts_page = get_permalink( get_option( 'page_for_posts' ) ); 642 } else { 643 $posts_page = home_url( '/' ); 644 } 645 } 646 647 $posts_page = esc_url( $posts_page ); 648 if ( 'list' === $parsed_args['style'] ) { 649 $output .= "<li class='cat-item-all'><a href='$posts_page'>$show_option_all</a></li>"; 650 } else { 651 $output .= "<a href='$posts_page'>$show_option_all</a>"; 652 } 653 } 654 655 if ( empty( $parsed_args['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) { 656 $current_term_object = get_queried_object(); 657 if ( $current_term_object && $parsed_args['taxonomy'] === $current_term_object->taxonomy ) { 658 $parsed_args['current_category'] = get_queried_object_id(); 659 } 660 } 661 662 if ( $parsed_args['hierarchical'] ) { 663 $depth = $parsed_args['depth']; 664 } else { 665 $depth = -1; // Flat. 666 } 667 $output .= walk_category_tree( $categories, $depth, $parsed_args ); 668 } 669 670 if ( $parsed_args['title_li'] && 'list' === $parsed_args['style'] 671 && ( ! empty( $categories ) || ! $parsed_args['hide_title_if_empty'] ) 672 ) { 673 $output .= '</ul></li>'; 674 } 675 676 /** 677 * Filters the HTML output of a taxonomy list. 678 * 679 * @since 2.1.0 680 * 681 * @param string $output HTML output. 682 * @param array|string $args An array or query string of taxonomy-listing arguments. See 683 * wp_list_categories() for information on accepted arguments. 684 */ 685 $html = apply_filters( 'wp_list_categories', $output, $args ); 686 687 if ( $parsed_args['echo'] ) { 688 echo $html; 689 } else { 690 return $html; 691 } 692 } 693 694 /** 695 * Displays a tag cloud. 696 * 697 * Outputs a list of tags in what is called a 'tag cloud', where the size of each tag 698 * is determined by how many times that particular tag has been assigned to posts. 699 * 700 * @since 2.3.0 701 * @since 2.8.0 Added the `taxonomy` argument. 702 * @since 4.8.0 Added the `show_count` argument. 703 * 704 * @param array|string $args { 705 * Optional. Array or string of arguments for displaying a tag cloud. See wp_generate_tag_cloud() 706 * and get_terms() for the full lists of arguments that can be passed in `$args`. 707 * 708 * @type int $number The number of tags to display. Accepts any positive integer 709 * or zero to return all. Default 45. 710 * @type string $link Whether to display term editing links or term permalinks. 711 * Accepts 'edit' and 'view'. Default 'view'. 712 * @type string $post_type The post type. Used to highlight the proper post type menu 713 * on the linked edit page. Defaults to the first post type 714 * associated with the taxonomy. 715 * @type bool $echo Whether or not to echo the return value. Default true. 716 * } 717 * @return string|string[]|null|void Tag cloud as a string, or as an array when the 'format' 718 * argument is 'array'. Null on failure. Nothing when 'echo' is 719 * true and 'format' is not 'array'. 720 * @phpstan-return ( 721 * $args is array{ format: 'array', ... } 722 * ? string[]|null 723 * : ( $args is array{ echo: false|0|''|'0', ... } 724 * ? string|null 725 * : ( $args is ''|'0'|array ? void : string|string[]|null ) ) 726 * ) 727 */ 728 function wp_tag_cloud( $args = '' ) { 729 $defaults = array( 730 'smallest' => 8, 731 'largest' => 22, 732 'unit' => 'pt', 733 'number' => 45, 734 'format' => 'flat', 735 'separator' => "\n", 736 'orderby' => 'name', 737 'order' => 'ASC', 738 'exclude' => '', 739 'include' => '', 740 'link' => 'view', 741 'taxonomy' => 'post_tag', 742 'post_type' => '', 743 'echo' => true, 744 'show_count' => 0, 745 ); 746 747 $args = wp_parse_args( $args, $defaults ); 748 749 $tags = get_terms( 750 array_merge( 751 $args, 752 array( 753 'orderby' => 'count', 754 'order' => 'DESC', 755 ) 756 ) 757 ); // Always query top tags. 758 759 if ( empty( $tags ) || is_wp_error( $tags ) ) { 760 return null; 761 } 762 763 foreach ( $tags as $key => $tag ) { 764 if ( 'edit' === $args['link'] ) { 765 $link = get_edit_term_link( $tag, $tag->taxonomy, $args['post_type'] ); 766 } else { 767 $link = get_term_link( $tag, $tag->taxonomy ); 768 } 769 770 if ( is_wp_error( $link ) ) { 771 return null; 772 } 773 774 $tags[ $key ]->link = $link; 775 $tags[ $key ]->id = $tag->term_id; 776 } 777 778 // Here's where those top tags get sorted according to $args. 779 $return = wp_generate_tag_cloud( $tags, $args ); 780 781 /** 782 * Filters the tag cloud output. 783 * 784 * @since 2.3.0 785 * 786 * @param string|string[] $return Tag cloud as a string or an array, depending on 'format' argument. 787 * @param array $args An array of tag cloud arguments. See wp_tag_cloud() 788 * for information on accepted arguments. 789 */ 790 $return = apply_filters( 'wp_tag_cloud', $return, $args ); 791 792 if ( 'array' === $args['format'] || empty( $args['echo'] ) ) { 793 return $return; 794 } 795 796 echo $return; 797 } 798 799 /** 800 * Default topic count scaling for tag links. 801 * 802 * @since 2.9.0 803 * 804 * @param int $count Number of posts with that tag. 805 * @return int Scaled count. 806 */ 807 function default_topic_count_scale( $count ) { 808 return (int) round( log10( $count + 1 ) * 100 ); 809 } 810 811 /** 812 * Generates a tag cloud (heatmap) from provided data. 813 * 814 * @todo Complete functionality. 815 * @since 2.3.0 816 * @since 4.8.0 Added the `show_count` argument. 817 * 818 * @param WP_Term[] $tags Array of WP_Term objects to generate the tag cloud for. 819 * @param string|array $args { 820 * Optional. Array or string of arguments for generating a tag cloud. 821 * 822 * @type int|float $smallest Smallest font size used to display tags. Paired 823 * with the value of `$unit`, to determine CSS text 824 * size unit. Default 8 (pt). 825 * @type int|float $largest Largest font size used to display tags. Paired 826 * with the value of `$unit`, to determine CSS text 827 * size unit. Default 22 (pt). 828 * @type string $unit CSS text size unit to use with the `$smallest` 829 * and `$largest` values. Accepts any valid CSS text 830 * size unit. Default 'pt'. 831 * @type int $number The number of tags to return. Accepts any 832 * positive integer or zero to return all. 833 * Default 0. 834 * @type string $format Format to display the tag cloud in. Accepts 'flat' 835 * (tags separated with spaces), 'list' (tags displayed 836 * in an unordered list), or 'array' (returns an array). 837 * Default 'flat'. 838 * @type string $separator HTML or text to separate the tags. Default "\n" (newline). 839 * @type string $orderby Value to order tags by. Accepts 'name' or 'count'. 840 * Default 'name'. The {@see 'tag_cloud_sort'} filter 841 * can also affect how tags are sorted. 842 * @type string $order How to order the tags. Accepts 'ASC' (ascending), 843 * 'DESC' (descending), or 'RAND' (random). Default 'ASC'. 844 * @type int|bool $filter Whether to enable filtering of the final output 845 * via {@see 'wp_generate_tag_cloud'}. Default 1. 846 * @type array $topic_count_text Nooped plural text from _n_noop() to supply to 847 * tag counts. Default null. 848 * @type callable $topic_count_text_callback Callback used to generate nooped plural text for 849 * tag counts based on the count. Default null. 850 * @type callable $topic_count_scale_callback Callback used to determine the tag count scaling 851 * value. Default default_topic_count_scale(). 852 * @type bool|int $show_count Whether to display the tag counts. Default 0. Accepts 853 * 0, 1, or their bool equivalents. 854 * } 855 * @return string|string[] Tag cloud as a string or an array, depending on 'format' argument. 856 * 857 * @phpstan-return ( $args is array{ format: 'array', ... } ? array<int, string> : string ) 858 */ 859 function wp_generate_tag_cloud( $tags, $args = '' ) { 860 $defaults = array( 861 'smallest' => 8, 862 'largest' => 22, 863 'unit' => 'pt', 864 'number' => 0, 865 'format' => 'flat', 866 'separator' => "\n", 867 'orderby' => 'name', 868 'order' => 'ASC', 869 'topic_count_text' => null, 870 'topic_count_text_callback' => null, 871 'topic_count_scale_callback' => 'default_topic_count_scale', 872 'filter' => 1, 873 'show_count' => 0, 874 ); 875 876 $args = wp_parse_args( $args, $defaults ); 877 878 $return = ( 'array' === $args['format'] ) ? array() : ''; 879 880 if ( empty( $tags ) ) { 881 return $return; 882 } 883 884 // Juggle topic counts. 885 if ( isset( $args['topic_count_text'] ) ) { 886 // First look for nooped plural support via topic_count_text. 887 $translate_nooped_plural = $args['topic_count_text']; 888 } elseif ( ! empty( $args['topic_count_text_callback'] ) ) { 889 // Look for the alternative callback style. Ignore the previous default. 890 if ( 'default_topic_count_text' === $args['topic_count_text_callback'] ) { 891 /* translators: %s: Number of items (tags). */ 892 $translate_nooped_plural = _n_noop( '%s item', '%s items' ); 893 } else { 894 $translate_nooped_plural = false; 895 } 896 } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) { 897 // If no callback exists, look for the old-style single_text and multiple_text arguments. 898 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingular,WordPress.WP.I18n.NonSingularStringLiteralPlural 899 $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] ); // @phpstan-ignore argument.type, argument.type (These are runtime values, so there is nothing to extract for translation.) 900 } else { 901 // This is the default for when no callback, plural, or argument is passed in. 902 /* translators: %s: Number of items (tags). */ 903 $translate_nooped_plural = _n_noop( '%s item', '%s items' ); 904 } 905 906 /** 907 * Filters how the items in a tag cloud are sorted. 908 * 909 * @since 2.8.0 910 * 911 * @param WP_Term[] $tags Ordered array of terms. 912 * @param array $args An array of tag cloud arguments. 913 */ 914 $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args ); 915 if ( empty( $tags_sorted ) ) { 916 return $return; 917 } 918 919 if ( $tags_sorted !== $tags ) { 920 $tags = $tags_sorted; 921 unset( $tags_sorted ); 922 } else { 923 if ( 'RAND' === $args['order'] ) { 924 shuffle( $tags ); 925 } else { 926 // SQL cannot save you; this is a second (potentially different) sort on a subset of data. 927 if ( 'name' === $args['orderby'] ) { 928 uasort( $tags, '_wp_object_name_sort_cb' ); 929 } else { 930 uasort( $tags, '_wp_object_count_sort_cb' ); 931 } 932 933 if ( 'DESC' === $args['order'] ) { 934 $tags = array_reverse( $tags, true ); 935 } 936 } 937 } 938 939 if ( $args['number'] > 0 ) { 940 $tags = array_slice( $tags, 0, $args['number'] ); 941 } 942 943 $counts = array(); 944 $real_counts = array(); // For the alt tag. 945 foreach ( (array) $tags as $key => $tag ) { 946 $real_counts[ $key ] = $tag->count; 947 $counts[ $key ] = call_user_func( $args['topic_count_scale_callback'], $tag->count ); 948 } 949 950 $min_count = min( $counts ); 951 $spread = max( $counts ) - $min_count; 952 if ( $spread <= 0 ) { 953 $spread = 1; 954 } 955 $font_spread = $args['largest'] - $args['smallest']; 956 if ( $font_spread < 0 ) { 957 $font_spread = 1; 958 } 959 $font_step = $font_spread / $spread; 960 961 $aria_label = false; 962 /* 963 * Determine whether to output an 'aria-label' attribute with the tag name and count. 964 * When tags have a different font size, they visually convey an important information 965 * that should be available to assistive technologies too. On the other hand, sometimes 966 * themes set up the Tag Cloud to display all tags with the same font size (setting 967 * the 'smallest' and 'largest' arguments to the same value). 968 * In order to always serve the same content to all users, the 'aria-label' gets printed out: 969 * - when tags have a different size 970 * - when the tag count is displayed (for example when users check the checkbox in the 971 * Tag Cloud widget), regardless of the tags font size 972 */ 973 if ( $args['show_count'] || $font_spread > 0 ) { 974 $aria_label = true; 975 } 976 977 // Assemble the data that will be used to generate the tag cloud markup. 978 $tags_data = array(); 979 foreach ( $tags as $key => $tag ) { 980 $tag_id = $tag->id ?? $key; 981 982 $count = $counts[ $key ]; 983 $real_count = $real_counts[ $key ]; 984 985 if ( $translate_nooped_plural ) { 986 $formatted_count = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) ); 987 } else { 988 $formatted_count = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args ); 989 } 990 991 $tags_data[] = array( 992 'id' => $tag_id, 993 'url' => $tag->link, 994 'role' => ( '#' !== $tag->link ) ? '' : ' role="button"', 995 'name' => $tag->name, 996 'formatted_count' => $formatted_count, 997 'slug' => $tag->slug, 998 'real_count' => $real_count, 999 'class' => 'tag-cloud-link tag-link-' . $tag_id, 1000 'font_size' => $args['smallest'] + ( $count - $min_count ) * $font_step, 1001 'aria_label' => $aria_label ? sprintf( ' aria-label="%1$s (%2$s)"', esc_attr( $tag->name ), esc_attr( $formatted_count ) ) : '', 1002 'show_count' => $args['show_count'] ? '<span class="tag-link-count"> (' . $real_count . ')</span>' : '', 1003 ); 1004 } 1005 1006 /** 1007 * Filters the data used to generate the tag cloud. 1008 * 1009 * @since 4.3.0 1010 * 1011 * @param array[] $tags_data An array of term data arrays for terms used to generate the tag cloud. 1012 */ 1013 $tags_data = apply_filters( 'wp_generate_tag_cloud_data', $tags_data ); 1014 1015 $a = array(); 1016 1017 // Generate the output links array. 1018 foreach ( $tags_data as $key => $tag_data ) { 1019 $class = $tag_data['class'] . ' tag-link-position-' . ( $key + 1 ); 1020 $a[] = sprintf( 1021 '<a href="%1$s"%2$s class="%3$s" style="font-size: %4$s;"%5$s>%6$s%7$s</a>', 1022 esc_url( $tag_data['url'] ), 1023 $tag_data['role'], 1024 esc_attr( $class ), 1025 esc_attr( str_replace( ',', '.', $tag_data['font_size'] ) . $args['unit'] ), 1026 $tag_data['aria_label'], 1027 esc_html( $tag_data['name'] ), 1028 $tag_data['show_count'] 1029 ); 1030 } 1031 1032 switch ( $args['format'] ) { 1033 case 'array': 1034 $return =& $a; 1035 break; 1036 case 'list': 1037 /* 1038 * Force role="list", as some browsers (sic: Safari 10) don't expose to assistive 1039 * technologies the default role when the list is styled with `list-style: none`. 1040 * Note: this is redundant but doesn't harm. 1041 */ 1042 $return = "<ul class='wp-tag-cloud' role='list'>\n\t<li>"; 1043 $return .= implode( "</li>\n\t<li>", $a ); 1044 $return .= "</li>\n</ul>\n"; 1045 break; 1046 default: 1047 $return = implode( $args['separator'], $a ); 1048 break; 1049 } 1050 1051 if ( $args['filter'] ) { 1052 /** 1053 * Filters the generated output of a tag cloud. 1054 * 1055 * The filter is only evaluated if a true value is passed 1056 * to the $filter argument in wp_generate_tag_cloud(). 1057 * 1058 * @since 2.3.0 1059 * 1060 * @see wp_generate_tag_cloud() 1061 * 1062 * @param string[]|string $return String containing the generated HTML tag cloud output 1063 * or an array of tag links if the 'format' argument 1064 * equals 'array'. 1065 * @param WP_Term[] $tags An array of terms used in the tag cloud. 1066 * @param array $args An array of wp_generate_tag_cloud() arguments. 1067 */ 1068 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args ); 1069 } else { 1070 return $return; 1071 } 1072 } 1073 1074 /** 1075 * Serves as a callback for comparing objects based on name. 1076 * 1077 * Used with `uasort()`. 1078 * 1079 * @since 3.1.0 1080 * @access private 1081 * 1082 * @param object $a The first object to compare. 1083 * @param object $b The second object to compare. 1084 * @return int Negative number if `$a->name` is less than `$b->name`, zero if they are equal, 1085 * or greater than zero if `$a->name` is greater than `$b->name`. 1086 */ 1087 function _wp_object_name_sort_cb( $a, $b ) { 1088 return strnatcasecmp( $a->name, $b->name ); 1089 } 1090 1091 /** 1092 * Serves as a callback for comparing objects based on count. 1093 * 1094 * Used with `uasort()`. 1095 * 1096 * @since 3.1.0 1097 * @access private 1098 * 1099 * @param object $a The first object to compare. 1100 * @param object $b The second object to compare. 1101 * @return int Negative number if `$a->count` is less than `$b->count`, zero if they are equal, 1102 * or greater than zero if `$a->count` is greater than `$b->count`. 1103 */ 1104 function _wp_object_count_sort_cb( $a, $b ) { 1105 return ( $a->count - $b->count ); 1106 } 1107 1108 // 1109 // Helper functions. 1110 // 1111 1112 /** 1113 * Retrieves HTML list content for category list. 1114 * 1115 * @since 2.1.0 1116 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it 1117 * to the function signature. 1118 * 1119 * @uses Walker_Category to create HTML list content. 1120 * @see Walker::walk() for parameters and return description. 1121 * 1122 * @param mixed ...$args Elements array, maximum hierarchical depth and optional additional arguments. 1123 * @return string 1124 */ 1125 function walk_category_tree( ...$args ) { 1126 // The user's options are the third parameter. 1127 if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) { 1128 $walker = new Walker_Category(); 1129 } else { 1130 /** 1131 * @var Walker $walker 1132 */ 1133 $walker = $args[2]['walker']; 1134 } 1135 return $walker->walk( ...$args ); 1136 } 1137 1138 /** 1139 * Retrieves HTML dropdown (select) content for category list. 1140 * 1141 * @since 2.1.0 1142 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it 1143 * to the function signature. 1144 * 1145 * @uses Walker_CategoryDropdown to create HTML dropdown content. 1146 * @see Walker::walk() for parameters and return description. 1147 * 1148 * @param mixed ...$args Elements array, maximum hierarchical depth and optional additional arguments. 1149 * @return string 1150 */ 1151 function walk_category_dropdown_tree( ...$args ) { 1152 // The user's options are the third parameter. 1153 if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) { 1154 $walker = new Walker_CategoryDropdown(); 1155 } else { 1156 /** 1157 * @var Walker $walker 1158 */ 1159 $walker = $args[2]['walker']; 1160 } 1161 return $walker->walk( ...$args ); 1162 } 1163 1164 // 1165 // Tags. 1166 // 1167 1168 /** 1169 * Retrieves the link to the tag. 1170 * 1171 * @since 2.3.0 1172 * 1173 * @see get_term_link() 1174 * 1175 * @param int|object $tag Tag ID or object. 1176 * @return string Link on success, empty string if tag does not exist. 1177 */ 1178 function get_tag_link( $tag ) { 1179 return get_category_link( $tag ); 1180 } 1181 1182 /** 1183 * Retrieves the tags for a post. 1184 * 1185 * @since 2.3.0 1186 * 1187 * @param int|WP_Post $post Post ID or object. 1188 * @return WP_Term[]|false|WP_Error Array of WP_Term objects on success, false if there are no terms 1189 * or the post does not exist, WP_Error on failure. 1190 */ 1191 function get_the_tags( $post = 0 ) { 1192 $terms = get_the_terms( $post, 'post_tag' ); 1193 1194 /** 1195 * Filters the array of tags for the given post. 1196 * 1197 * @since 2.3.0 1198 * 1199 * @see get_the_terms() 1200 * 1201 * @param WP_Term[]|false|WP_Error $terms Array of WP_Term objects on success, false if there are no terms 1202 * or the post does not exist, WP_Error on failure. 1203 */ 1204 return apply_filters( 'get_the_tags', $terms ); 1205 } 1206 1207 /** 1208 * Retrieves the tags for a post formatted as a string. 1209 * 1210 * @since 2.3.0 1211 * 1212 * @param string $before Optional. String to use before the tags. Default empty. 1213 * @param string $sep Optional. String to use between the tags. Default empty. 1214 * @param string $after Optional. String to use after the tags. Default empty. 1215 * @param int $post_id Optional. Post ID. Defaults to the current post ID. 1216 * @return string|false|WP_Error A list of tags on success, false if there are no terms, 1217 * WP_Error on failure. 1218 */ 1219 function get_the_tag_list( $before = '', $sep = '', $after = '', $post_id = 0 ) { 1220 $tag_list = get_the_term_list( $post_id, 'post_tag', $before, $sep, $after ); 1221 1222 /** 1223 * Filters the tags list for a given post. 1224 * 1225 * @since 2.3.0 1226 * 1227 * @param string|false|WP_Error $tag_list A list of tags on success, false if there are no terms, 1228 * WP_Error on failure. 1229 * @param string $before String to use before the tags. 1230 * @param string $sep String to use between the tags. 1231 * @param string $after String to use after the tags. 1232 * @param int $post_id Post ID. 1233 */ 1234 return apply_filters( 'the_tags', $tag_list, $before, $sep, $after, $post_id ); 1235 } 1236 1237 /** 1238 * Displays the tags for a post. 1239 * 1240 * @since 2.3.0 1241 * 1242 * @param string $before Optional. String to use before the tags. Defaults to 'Tags:'. 1243 * @param string $sep Optional. String to use between the tags. Default ', '. 1244 * @param string $after Optional. String to use after the tags. Default empty. 1245 */ 1246 function the_tags( $before = null, $sep = ', ', $after = '' ) { 1247 if ( null === $before ) { 1248 $before = __( 'Tags:' ) . ' '; 1249 } 1250 1251 $the_tags = get_the_tag_list( $before, $sep, $after ); 1252 1253 if ( ! is_wp_error( $the_tags ) ) { 1254 echo $the_tags; 1255 } 1256 } 1257 1258 /** 1259 * Retrieves tag description. 1260 * 1261 * @since 2.8.0 1262 * 1263 * @param int $tag Optional. Tag ID. Defaults to the current tag ID. 1264 * @return string Tag description, if available. 1265 */ 1266 function tag_description( $tag = 0 ) { 1267 return term_description( $tag ); 1268 } 1269 1270 /** 1271 * Retrieves term description. 1272 * 1273 * @since 2.8.0 1274 * @since 4.9.2 The `$taxonomy` parameter was deprecated. 1275 * 1276 * @param int $term Optional. Term ID. Defaults to the current term ID. 1277 * @param mixed $deprecated Not used. 1278 * @return string Term description, if available. 1279 */ 1280 function term_description( $term = 0, $deprecated = null ) { 1281 if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) { 1282 $term = get_queried_object(); 1283 if ( $term ) { 1284 $term = $term->term_id; 1285 } 1286 } 1287 1288 $description = get_term_field( 'description', $term ); 1289 1290 return is_wp_error( $description ) ? '' : $description; 1291 } 1292 1293 /** 1294 * Retrieves the terms of the taxonomy that are attached to the post. 1295 * 1296 * @since 2.5.0 1297 * 1298 * @param int|WP_Post $post Post ID or object. 1299 * @param string $taxonomy Taxonomy name. 1300 * @return WP_Term[]|false|WP_Error Array of WP_Term objects on success, false if there are no terms 1301 * or the post does not exist, WP_Error on failure. 1302 */ 1303 function get_the_terms( $post, $taxonomy ) { 1304 $post = get_post( $post ); 1305 1306 if ( ! $post ) { 1307 return false; 1308 } 1309 1310 $terms = get_object_term_cache( $post->ID, $taxonomy ); 1311 1312 if ( false === $terms ) { 1313 $terms = wp_get_object_terms( $post->ID, $taxonomy ); 1314 if ( ! is_wp_error( $terms ) ) { 1315 $term_ids = wp_list_pluck( $terms, 'term_id' ); 1316 wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' ); 1317 } 1318 } 1319 1320 /** 1321 * Filters the list of terms attached to the given post. 1322 * 1323 * @since 3.1.0 1324 * 1325 * @param WP_Term[]|WP_Error $terms Array of attached terms, or WP_Error on failure. 1326 * @param int $post_id Post ID. 1327 * @param string $taxonomy Name of the taxonomy. 1328 */ 1329 $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy ); 1330 1331 if ( empty( $terms ) ) { 1332 return false; 1333 } 1334 1335 return $terms; 1336 } 1337 1338 /** 1339 * Retrieves a post's terms as a list with specified format. 1340 * 1341 * Terms are linked to their respective term listing pages. 1342 * 1343 * @since 2.5.0 1344 * 1345 * @param int $post_id Post ID. 1346 * @param string $taxonomy Taxonomy name. 1347 * @param string $before Optional. String to use before the terms. Default empty. 1348 * @param string $sep Optional. String to use between the terms. Default empty. 1349 * @param string $after Optional. String to use after the terms. Default empty. 1350 * @return string|false|WP_Error A list of terms on success, false if there are no terms, 1351 * WP_Error on failure. 1352 */ 1353 function get_the_term_list( $post_id, $taxonomy, $before = '', $sep = '', $after = '' ) { 1354 $terms = get_the_terms( $post_id, $taxonomy ); 1355 1356 if ( is_wp_error( $terms ) ) { 1357 return $terms; 1358 } 1359 1360 if ( empty( $terms ) ) { 1361 return false; 1362 } 1363 1364 $links = array(); 1365 1366 foreach ( $terms as $term ) { 1367 $link = get_term_link( $term, $taxonomy ); 1368 if ( is_wp_error( $link ) ) { 1369 return $link; 1370 } 1371 $links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>'; 1372 } 1373 1374 /** 1375 * Filters the term links for a given taxonomy. 1376 * 1377 * The dynamic portion of the hook name, `$taxonomy`, refers 1378 * to the taxonomy slug. 1379 * 1380 * Possible hook names include: 1381 * 1382 * - `term_links-category` 1383 * - `term_links-post_tag` 1384 * - `term_links-post_format` 1385 * 1386 * @since 2.5.0 1387 * 1388 * @param string[] $links An array of term links. 1389 */ 1390 $term_links = apply_filters( "term_links-{$taxonomy}", $links ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 1391 1392 return $before . implode( $sep, $term_links ) . $after; 1393 } 1394 1395 /** 1396 * Retrieves term parents with separator. 1397 * 1398 * @since 4.8.0 1399 * 1400 * @param int $term_id Term ID. 1401 * @param string $taxonomy Taxonomy name. 1402 * @param string|array $args { 1403 * Array of optional arguments. 1404 * 1405 * @type string $format Use term names or slugs for display. Accepts 'name' or 'slug'. 1406 * Default 'name'. 1407 * @type string $separator Separator for between the terms. Default '/'. 1408 * @type bool $link Whether to format as a link. Default true. 1409 * @type bool $inclusive Include the term to get the parents for. Default true. 1410 * } 1411 * @return string|WP_Error A list of term parents on success, WP_Error or empty string on failure. 1412 */ 1413 function get_term_parents_list( $term_id, $taxonomy, $args = array() ) { 1414 $list = ''; 1415 $term = get_term( $term_id, $taxonomy ); 1416 1417 if ( is_wp_error( $term ) ) { 1418 return $term; 1419 } 1420 1421 if ( ! $term ) { 1422 return $list; 1423 } 1424 1425 $term_id = $term->term_id; 1426 1427 $defaults = array( 1428 'format' => 'name', 1429 'separator' => '/', 1430 'link' => true, 1431 'inclusive' => true, 1432 ); 1433 1434 $args = wp_parse_args( $args, $defaults ); 1435 1436 foreach ( array( 'link', 'inclusive' ) as $bool ) { 1437 $args[ $bool ] = wp_validate_boolean( $args[ $bool ] ); 1438 } 1439 1440 $parents = get_ancestors( $term_id, $taxonomy, 'taxonomy' ); 1441 1442 if ( $args['inclusive'] ) { 1443 array_unshift( $parents, $term_id ); 1444 } 1445 1446 foreach ( array_reverse( $parents ) as $term_id ) { 1447 $parent = get_term( $term_id, $taxonomy ); 1448 $name = ( 'slug' === $args['format'] ) ? $parent->slug : $parent->name; 1449 1450 if ( $args['link'] ) { 1451 $list .= '<a href="' . esc_url( get_term_link( $parent->term_id, $taxonomy ) ) . '">' . $name . '</a>' . $args['separator']; 1452 } else { 1453 $list .= $name . $args['separator']; 1454 } 1455 } 1456 1457 return $list; 1458 } 1459 1460 /** 1461 * Displays the terms for a post in a list. 1462 * 1463 * @since 2.5.0 1464 * 1465 * @param int $post_id Post ID. 1466 * @param string $taxonomy Taxonomy name. 1467 * @param string $before Optional. String to use before the terms. Default empty. 1468 * @param string $sep Optional. String to use between the terms. Default ', '. 1469 * @param string $after Optional. String to use after the terms. Default empty. 1470 * @return void|false Void on success, false on failure. 1471 */ 1472 function the_terms( $post_id, $taxonomy, $before = '', $sep = ', ', $after = '' ) { 1473 $term_list = get_the_term_list( $post_id, $taxonomy, $before, $sep, $after ); 1474 1475 if ( is_wp_error( $term_list ) ) { 1476 return false; 1477 } 1478 1479 /** 1480 * Filters the list of terms to display. 1481 * 1482 * @since 2.9.0 1483 * 1484 * @param string $term_list List of terms to display. 1485 * @param string $taxonomy The taxonomy name. 1486 * @param string $before String to use before the terms. 1487 * @param string $sep String to use between the terms. 1488 * @param string $after String to use after the terms. 1489 */ 1490 echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after ); 1491 } 1492 1493 /** 1494 * Checks if the current post has any of given category. 1495 * 1496 * The given categories are checked against the post's categories' term_ids, names and slugs. 1497 * Categories given as integers will only be checked against the post's categories' term_ids. 1498 * 1499 * If no categories are given, determines if post has any categories. 1500 * 1501 * @since 3.1.0 1502 * 1503 * @param string|int|array $category Optional. The category name/term_id/slug, 1504 * or an array of them to check for. Default empty. 1505 * @param int|WP_Post|null $post Optional. Post to check. Defaults to the current post. 1506 * @return bool True if the current post has any of the given categories 1507 * (or any category, if no category specified). False otherwise. 1508 */ 1509 function has_category( $category = '', $post = null ) { 1510 return has_term( $category, 'category', $post ); 1511 } 1512 1513 /** 1514 * Checks if the current post has any of given tags. 1515 * 1516 * The given tags are checked against the post's tags' term_ids, names and slugs. 1517 * Tags given as integers will only be checked against the post's tags' term_ids. 1518 * 1519 * If no tags are given, determines if post has any tags. 1520 * 1521 * For more information on this and similar theme functions, check out 1522 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 1523 * Conditional Tags} article in the Theme Developer Handbook. 1524 * 1525 * @since 2.6.0 1526 * @since 2.7.0 Tags given as integers are only checked against 1527 * the post's tags' term_ids, not names or slugs. 1528 * @since 2.7.0 Can be used outside of the WordPress Loop if `$post` is provided. 1529 * 1530 * @param string|int|array $tag Optional. The tag name/term_id/slug, 1531 * or an array of them to check for. Default empty. 1532 * @param int|WP_Post|null $post Optional. Post to check. Defaults to the current post. 1533 * @return bool True if the current post has any of the given tags 1534 * (or any tag, if no tag specified). False otherwise. 1535 */ 1536 function has_tag( $tag = '', $post = null ) { 1537 return has_term( $tag, 'post_tag', $post ); 1538 } 1539 1540 /** 1541 * Checks if the current post has any of given terms. 1542 * 1543 * The given terms are checked against the post's terms' term_ids, names and slugs. 1544 * Terms given as integers will only be checked against the post's terms' term_ids. 1545 * 1546 * If no terms are given, determines if post has any terms. 1547 * 1548 * @since 3.1.0 1549 * 1550 * @param string|int|array $term Optional. The term name/term_id/slug, 1551 * or an array of them to check for. Default empty. 1552 * @param string $taxonomy Optional. Taxonomy name. Default empty. 1553 * @param int|WP_Post|null $post Optional. Post to check. Defaults to the current post. 1554 * @return bool True if the current post has any of the given terms 1555 * (or any term, if no term specified). False otherwise. 1556 */ 1557 function has_term( $term = '', $taxonomy = '', $post = null ) { 1558 $post = get_post( $post ); 1559 1560 if ( ! $post ) { 1561 return false; 1562 } 1563 1564 $r = is_object_in_term( $post->ID, $taxonomy, $term ); 1565 if ( is_wp_error( $r ) ) { 1566 return false; 1567 } 1568 1569 return $r; 1570 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Nov 21 08:20:02 2026 | Cross-referenced by PHPXref |