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


Generated: Fri Oct 30 01:00:03 2020 Cross-referenced by PHPXref 0.7.1