[ Index ]

PHP Cross Reference of WordPress

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


Generated: Tue Jun 2 01:00:03 2020 Cross-referenced by PHPXref 0.7.1