[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> category-template.php (source)

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


Generated : Sat Sep 12 08:20:32 2026 Cross-referenced by PHPXref