category-template.php 51 KB

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