Keine Beschreibung

category-template.php 55KB

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