category-template.php 50 KB

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