You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1327 lines
47 KiB

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