category-template.php 56 KB

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