PageRenderTime 55ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/category-template.php

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