PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/category-template.php

https://github.com/markjaquith/WordPress
PHP | 1539 lines | 664 code | 157 blank | 718 comment | 140 complexity | dbbbd851edaf23ba328efd0507ecef5b MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file