PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/category-template.php

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 1405 lines | 590 code | 149 blank | 666 comment | 162 complexity | ac21ffd2a75ab94f1600dd550c804ba9 MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  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 list of arguments is below:
  264. * 'show_option_all' (string) - Text to display for showing all categories.
  265. * 'show_option_none' (string) - Text to display for showing no categories.
  266. * 'option_none_value' (mixed) - Value to use when no category is selected.
  267. * 'orderby' (string) default is 'ID' - What column to use for ordering the
  268. * categories.
  269. * 'order' (string) default is 'ASC' - What direction to order categories.
  270. * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
  271. * in the category.
  272. * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
  273. * don't have any posts attached to them.
  274. * 'child_of' (int) default is 0 - See {@link get_categories()}.
  275. * 'exclude' (string) - See {@link get_categories()}.
  276. * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
  277. * 'depth' (int) - The max depth.
  278. * 'tab_index' (int) - Tab index for select element.
  279. * 'name' (string) - The name attribute value for select element.
  280. * 'id' (string) - The ID attribute value for select element. Defaults to name if omitted.
  281. * 'class' (string) - The class attribute value for select element.
  282. * 'selected' (int) - Which category ID is selected.
  283. * 'taxonomy' (string) - The name of the taxonomy to retrieve. Defaults to category.
  284. *
  285. * The 'hierarchical' argument, which is disabled by default, will override the
  286. * depth argument, unless it is true. When the argument is false, it will
  287. * display all of the categories. When it is enabled it will use the value in
  288. * the 'depth' argument.
  289. *
  290. * @since 2.1.0
  291. *
  292. * @param string|array $args Optional. Override default arguments.
  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' => '', 'show_option_none' => '',
  298. 'orderby' => 'id', 'order' => 'ASC',
  299. 'show_count' => 0,
  300. 'hide_empty' => 1, 'child_of' => 0,
  301. 'exclude' => '', 'echo' => 1,
  302. 'selected' => 0, 'hierarchical' => 0,
  303. 'name' => 'cat', 'id' => '',
  304. 'class' => 'postform', 'depth' => 0,
  305. 'tab_index' => 0, 'taxonomy' => 'category',
  306. 'hide_if_empty' => false, 'option_none_value' => -1
  307. );
  308. $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
  309. // Back compat.
  310. if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
  311. _deprecated_argument( __FUNCTION__, '3.0', '' );
  312. $args['taxonomy'] = 'link_category';
  313. }
  314. $r = wp_parse_args( $args, $defaults );
  315. $option_none_value = $r['option_none_value'];
  316. if ( ! isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
  317. $r['pad_counts'] = true;
  318. }
  319. $tab_index = $r['tab_index'];
  320. $tab_index_attribute = '';
  321. if ( (int) $tab_index > 0 ) {
  322. $tab_index_attribute = " tabindex=\"$tab_index\"";
  323. }
  324. $categories = get_terms( $r['taxonomy'], $r );
  325. $name = esc_attr( $r['name'] );
  326. $class = esc_attr( $r['class'] );
  327. $id = $r['id'] ? esc_attr( $r['id'] ) : $name;
  328. if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
  329. $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
  330. } else {
  331. $output = '';
  332. }
  333. if ( empty( $categories ) && ! $r['hide_if_empty'] && ! empty( $r['show_option_none'] ) ) {
  334. /**
  335. * Filter a taxonomy drop-down display element.
  336. *
  337. * A variety of taxonomy drop-down display elements can be modified
  338. * just prior to display via this filter. Filterable arguments include
  339. * 'show_option_none', 'show_option_all', and various forms of the
  340. * term name.
  341. *
  342. * @since 1.2.0
  343. *
  344. * @see wp_dropdown_categories()
  345. *
  346. * @param string $element Taxonomy element to list.
  347. */
  348. $show_option_none = apply_filters( 'list_cats', $r['show_option_none'] );
  349. $output .= "\t<option value='" . esc_attr( $option_none_value ) . "' selected='selected'>$show_option_none</option>\n";
  350. }
  351. if ( ! empty( $categories ) ) {
  352. if ( $r['show_option_all'] ) {
  353. /** This filter is documented in wp-includes/category-template.php */
  354. $show_option_all = apply_filters( 'list_cats', $r['show_option_all'] );
  355. $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
  356. $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
  357. }
  358. if ( $r['show_option_none'] ) {
  359. /** This filter is documented in wp-includes/category-template.php */
  360. $show_option_none = apply_filters( 'list_cats', $r['show_option_none'] );
  361. $selected = selected( $option_none_value, $r['selected'], false );
  362. $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$selected>$show_option_none</option>\n";
  363. }
  364. if ( $r['hierarchical'] ) {
  365. $depth = $r['depth']; // Walk the full depth.
  366. } else {
  367. $depth = -1; // Flat.
  368. }
  369. $output .= walk_category_dropdown_tree( $categories, $depth, $r );
  370. }
  371. if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
  372. $output .= "</select>\n";
  373. }
  374. /**
  375. * Filter the taxonomy drop-down output.
  376. *
  377. * @since 2.1.0
  378. *
  379. * @param string $output HTML output.
  380. * @param array $r Arguments used to build the drop-down.
  381. */
  382. $output = apply_filters( 'wp_dropdown_cats', $output, $r );
  383. if ( $r['echo'] ) {
  384. echo $output;
  385. }
  386. return $output;
  387. }
  388. /**
  389. * Display or retrieve the HTML list of categories.
  390. *
  391. * The list of arguments is below:
  392. * 'show_option_all' (string) - Text to display for showing all categories.
  393. * 'orderby' (string) default is 'ID' - What column to use for ordering the
  394. * categories.
  395. * 'order' (string) default is 'ASC' - What direction to order categories.
  396. * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
  397. * in the category.
  398. * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
  399. * don't have any posts attached to them.
  400. * 'use_desc_for_title' (bool|int) default is 1 - Whether to use the
  401. * category description as the title attribute.
  402. * 'feed' - See {@link get_categories()}.
  403. * 'feed_type' - See {@link get_categories()}.
  404. * 'feed_image' - See {@link get_categories()}.
  405. * 'child_of' (int) default is 0 - See {@link get_categories()}.
  406. * 'exclude' (string) - See {@link get_categories()}.
  407. * 'exclude_tree' (string) - See {@link get_categories()}.
  408. * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
  409. * 'current_category' (int) - See {@link get_categories()}.
  410. * 'hierarchical' (bool) - See {@link get_categories()}.
  411. * 'title_li' (string) - See {@link get_categories()}.
  412. * 'depth' (int) - The max depth.
  413. *
  414. * @since 2.1.0
  415. *
  416. * @param string|array $args Optional. Override default arguments.
  417. * @return false|null|string HTML content only if 'echo' argument is 0.
  418. */
  419. function wp_list_categories( $args = '' ) {
  420. $defaults = array(
  421. 'show_option_all' => '', 'show_option_none' => __('No categories'),
  422. 'orderby' => 'name', 'order' => 'ASC',
  423. 'style' => 'list',
  424. 'show_count' => 0, 'hide_empty' => 1,
  425. 'use_desc_for_title' => 1, 'child_of' => 0,
  426. 'feed' => '', 'feed_type' => '',
  427. 'feed_image' => '', 'exclude' => '',
  428. 'exclude_tree' => '', 'current_category' => 0,
  429. 'hierarchical' => true, 'title_li' => __( 'Categories' ),
  430. 'echo' => 1, 'depth' => 0,
  431. 'taxonomy' => 'category'
  432. );
  433. $r = wp_parse_args( $args, $defaults );
  434. if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
  435. $r['pad_counts'] = true;
  436. if ( true == $r['hierarchical'] ) {
  437. $r['exclude_tree'] = $r['exclude'];
  438. $r['exclude'] = '';
  439. }
  440. if ( ! isset( $r['class'] ) )
  441. $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
  442. if ( ! taxonomy_exists( $r['taxonomy'] ) ) {
  443. return false;
  444. }
  445. $show_option_all = $r['show_option_all'];
  446. $show_option_none = $r['show_option_none'];
  447. $categories = get_categories( $r );
  448. $output = '';
  449. if ( $r['title_li'] && 'list' == $r['style'] ) {
  450. $output = '<li class="' . esc_attr( $r['class'] ) . '">' . $r['title_li'] . '<ul>';
  451. }
  452. if ( empty( $categories ) ) {
  453. if ( ! empty( $show_option_none ) ) {
  454. if ( 'list' == $r['style'] ) {
  455. $output .= '<li class="cat-item-none">' . $show_option_none . '</li>';
  456. } else {
  457. $output .= $show_option_none;
  458. }
  459. }
  460. } else {
  461. if ( ! empty( $show_option_all ) ) {
  462. $posts_page = ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) ? get_permalink( get_option( 'page_for_posts' ) ) : home_url( '/' );
  463. $posts_page = esc_url( $posts_page );
  464. if ( 'list' == $r['style'] ) {
  465. $output .= "<li class='cat-item-all'><a href='$posts_page'>$show_option_all</a></li>";
  466. } else {
  467. $output .= "<a href='$posts_page'>$show_option_all</a>";
  468. }
  469. }
  470. if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
  471. $current_term_object = get_queried_object();
  472. if ( $current_term_object && $r['taxonomy'] === $current_term_object->taxonomy ) {
  473. $r['current_category'] = get_queried_object_id();
  474. }
  475. }
  476. if ( $r['hierarchical'] ) {
  477. $depth = $r['depth'];
  478. } else {
  479. $depth = -1; // Flat.
  480. }
  481. $output .= walk_category_tree( $categories, $depth, $r );
  482. }
  483. if ( $r['title_li'] && 'list' == $r['style'] )
  484. $output .= '</ul></li>';
  485. /**
  486. * Filter the HTML output of a taxonomy list.
  487. *
  488. * @since 2.1.0
  489. *
  490. * @param string $output HTML output.
  491. * @param array $args An array of taxonomy-listing arguments.
  492. */
  493. $html = apply_filters( 'wp_list_categories', $output, $args );
  494. if ( $r['echo'] ) {
  495. echo $html;
  496. } else {
  497. return $html;
  498. }
  499. }
  500. /**
  501. * Display tag cloud.
  502. *
  503. * The text size is set by the 'smallest' and 'largest' arguments, which will
  504. * use the 'unit' argument value for the CSS text size unit. The 'format'
  505. * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
  506. * 'format' argument will separate tags with spaces. The list value for the
  507. * 'format' argument will format the tags in a UL HTML list. The array value for
  508. * the 'format' argument will return in PHP array type format.
  509. *
  510. * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
  511. * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
  512. *
  513. * The 'number' argument is how many tags to return. By default, the limit will
  514. * be to return the top 45 tags in the tag cloud list.
  515. *
  516. * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
  517. * text for the tooltip of the tag link.
  518. *
  519. * The 'topic_count_text_callback' argument is a function, which given the count
  520. * of the posts with that tag returns a text for the tooltip of the tag link.
  521. *
  522. * The 'post_type' argument is used only when 'link' is set to 'edit'. It determines the post_type
  523. * passed to edit.php for the popular tags edit links.
  524. *
  525. * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
  526. * function. Only one should be used, because only one will be used and the
  527. * other ignored, if they are both set.
  528. *
  529. * @since 2.3.0
  530. *
  531. * @param array|string|null $args Optional. Override default arguments.
  532. * @return null|false Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
  533. */
  534. function wp_tag_cloud( $args = '' ) {
  535. $defaults = array(
  536. 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
  537. 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
  538. 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true
  539. );
  540. $args = wp_parse_args( $args, $defaults );
  541. $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
  542. if ( empty( $tags ) || is_wp_error( $tags ) )
  543. return;
  544. foreach ( $tags as $key => $tag ) {
  545. if ( 'edit' == $args['link'] )
  546. $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] );
  547. else
  548. $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
  549. if ( is_wp_error( $link ) )
  550. return false;
  551. $tags[ $key ]->link = $link;
  552. $tags[ $key ]->id = $tag->term_id;
  553. }
  554. $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
  555. /**
  556. * Filter the tag cloud output.
  557. *
  558. * @since 2.3.0
  559. *
  560. * @param string $return HTML output of the tag cloud.
  561. * @param array $args An array of tag cloud arguments.
  562. */
  563. $return = apply_filters( 'wp_tag_cloud', $return, $args );
  564. if ( 'array' == $args['format'] || empty($args['echo']) )
  565. return $return;
  566. echo $return;
  567. }
  568. /**
  569. * Default topic count scaling for tag links
  570. *
  571. * @param integer $count number of posts with that tag
  572. * @return integer scaled count
  573. */
  574. function default_topic_count_scale( $count ) {
  575. return round(log10($count + 1) * 100);
  576. }
  577. /**
  578. * Generates a tag cloud (heatmap) from provided data.
  579. *
  580. * The text size is set by the 'smallest' and 'largest' arguments, which will
  581. * use the 'unit' argument value for the CSS text size unit. The 'format'
  582. * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
  583. * 'format' argument will separate tags with spaces. The list value for the
  584. * 'format' argument will format the tags in a UL HTML list. The array value for
  585. * the 'format' argument will return in PHP array type format.
  586. *
  587. * The 'tag_cloud_sort' filter allows you to override the sorting.
  588. * Passed to the filter: $tags array and $args array, has to return the $tags array
  589. * after sorting it.
  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' or
  593. * 'RAND'.
  594. *
  595. * The 'number' argument is how many tags to return. By default, the limit will
  596. * be to return the entire tag cloud list.
  597. *
  598. * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
  599. * text for the tooltip of the tag link.
  600. *
  601. * The 'topic_count_text_callback' argument is a function, which given the count
  602. * of the posts with that tag returns a text for the tooltip of the tag link.
  603. *
  604. * @todo Complete functionality.
  605. * @since 2.3.0
  606. *
  607. * @param array $tags List of tags.
  608. * @param string|array $args Optional, override default arguments.
  609. * @return string|array Tag cloud as a string or an array, depending on 'format' argument.
  610. */
  611. function wp_generate_tag_cloud( $tags, $args = '' ) {
  612. $defaults = array(
  613. 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
  614. 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
  615. 'topic_count_text' => null, 'topic_count_text_callback' => null,
  616. 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
  617. );
  618. $args = wp_parse_args( $args, $defaults );
  619. $return = ( 'array' === $args['format'] ) ? array() : '';
  620. if ( empty( $tags ) ) {
  621. return $return;
  622. }
  623. // Juggle topic count tooltips:
  624. if ( isset( $args['topic_count_text'] ) ) {
  625. // First look for nooped plural support via topic_count_text.
  626. $translate_nooped_plural = $args['topic_count_text'];
  627. } elseif ( ! empty( $args['topic_count_text_callback'] ) ) {
  628. // Look for the alternative callback style. Ignore the previous default.
  629. if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) {
  630. $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
  631. } else {
  632. $translate_nooped_plural = false;
  633. }
  634. } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
  635. // If no callback exists, look for the old-style single_text and multiple_text arguments.
  636. $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] );
  637. } else {
  638. // This is the default for when no callback, plural, or argument is passed in.
  639. $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
  640. }
  641. /**
  642. * Filter how the items in a tag cloud are sorted.
  643. *
  644. * @since 2.8.0
  645. *
  646. * @param array $tags Ordered array of terms.
  647. * @param array $args An array of tag cloud arguments.
  648. */
  649. $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
  650. if ( empty( $tags_sorted ) ) {
  651. return $return;
  652. }
  653. if ( $tags_sorted !== $tags ) {
  654. $tags = $tags_sorted;
  655. unset( $tags_sorted );
  656. } else {
  657. if ( 'RAND' === $args['order'] ) {
  658. shuffle( $tags );
  659. } else {
  660. // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
  661. if ( 'name' === $args['orderby'] ) {
  662. uasort( $tags, '_wp_object_name_sort_cb' );
  663. } else {
  664. uasort( $tags, '_wp_object_count_sort_cb' );
  665. }
  666. if ( 'DESC' === $args['order'] ) {
  667. $tags = array_reverse( $tags, true );
  668. }
  669. }
  670. }
  671. if ( $args['number'] > 0 )
  672. $tags = array_slice( $tags, 0, $args['number'] );
  673. $counts = array();
  674. $real_counts = array(); // For the alt tag
  675. foreach ( (array) $tags as $key => $tag ) {
  676. $real_counts[ $key ] = $tag->count;
  677. $counts[ $key ] = call_user_func( $args['topic_count_scale_callback'], $tag->count );
  678. }
  679. $min_count = min( $counts );
  680. $spread = max( $counts ) - $min_count;
  681. if ( $spread <= 0 )
  682. $spread = 1;
  683. $font_spread = $args['largest'] - $args['smallest'];
  684. if ( $font_spread < 0 )
  685. $font_spread = 1;
  686. $font_step = $font_spread / $spread;
  687. $a = array();
  688. foreach ( $tags as $key => $tag ) {
  689. $count = $counts[ $key ];
  690. $real_count = $real_counts[ $key ];
  691. $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
  692. $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
  693. $tag_name = $tags[ $key ]->name;
  694. if ( $translate_nooped_plural ) {
  695. $title_attribute = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
  696. } else {
  697. $title_attribute = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args );
  698. }
  699. $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $title_attribute ) . "' style='font-size: " .
  700. str_replace( ',', '.', ( $args['smallest'] + ( ( $count - $min_count ) * $font_step ) ) )
  701. . $args['unit'] . ";'>$tag_name</a>";
  702. }
  703. switch ( $args['format'] ) {
  704. case 'array' :
  705. $return =& $a;
  706. break;
  707. case 'list' :
  708. $return = "<ul class='wp-tag-cloud'>\n\t<li>";
  709. $return .= join( "</li>\n\t<li>", $a );
  710. $return .= "</li>\n</ul>\n";
  711. break;
  712. default :
  713. $return = join( $args['separator'], $a );
  714. break;
  715. }
  716. if ( $args['filter'] ) {
  717. /**
  718. * Filter the generated output of a tag cloud.
  719. *
  720. * The filter is only evaluated if a true value is passed
  721. * to the $filter argument in wp_generate_tag_cloud().
  722. *
  723. * @since 2.3.0
  724. *
  725. * @see wp_generate_tag_cloud()
  726. *
  727. * @param array|string $return String containing the generated HTML tag cloud output
  728. * or an array of tag links if the 'format' argument
  729. * equals 'array'.
  730. * @param array $tags An array of terms used in the tag cloud.
  731. * @param array $args An array of wp_generate_tag_cloud() arguments.
  732. */
  733. return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
  734. }
  735. else
  736. return $return;
  737. }
  738. /**
  739. * Callback for comparing objects based on name
  740. *
  741. * @since 3.1.0
  742. * @access private
  743. */
  744. function _wp_object_name_sort_cb( $a, $b ) {
  745. return strnatcasecmp( $a->name, $b->name );
  746. }
  747. /**
  748. * Callback for comparing objects based on count
  749. *
  750. * @since 3.1.0
  751. * @access private
  752. */
  753. function _wp_object_count_sort_cb( $a, $b ) {
  754. return ( $a->count > $b->count );
  755. }
  756. //
  757. // Helper functions
  758. //
  759. /**
  760. * Retrieve HTML list content for category list.
  761. *
  762. * @uses Walker_Category to create HTML list content.
  763. * @since 2.1.0
  764. * @see Walker_Category::walk() for parameters and return description.
  765. */
  766. function walk_category_tree() {
  767. $args = func_get_args();
  768. // the user's options are the third parameter
  769. if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
  770. $walker = new Walker_Category;
  771. else
  772. $walker = $args[2]['walker'];
  773. return call_user_func_array(array( &$walker, 'walk' ), $args );
  774. }
  775. /**
  776. * Retrieve HTML dropdown (select) content for category list.
  777. *
  778. * @uses Walker_CategoryDropdown to create HTML dropdown content.
  779. * @since 2.1.0
  780. * @see Walker_CategoryDropdown::walk() for parameters and return description.
  781. */
  782. function walk_category_dropdown_tree() {
  783. $args = func_get_args();
  784. // the user's options are the third parameter
  785. if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
  786. $walker = new Walker_CategoryDropdown;
  787. else
  788. $walker = $args[2]['walker'];
  789. return call_user_func_array(array( &$walker, 'walk' ), $args );
  790. }
  791. /**
  792. * Create HTML list of categories.
  793. *
  794. * @package WordPress
  795. * @since 2.1.0
  796. * @uses Walker
  797. */
  798. class Walker_Category extends Walker {
  799. /**
  800. * What the class handles.
  801. *
  802. * @see Walker::$tree_type
  803. * @since 2.1.0
  804. * @var string
  805. */
  806. public $tree_type = 'category';
  807. /**
  808. * Database fields to use.
  809. *
  810. * @see Walker::$db_fields
  811. * @since 2.1.0
  812. * @todo Decouple this
  813. * @var array
  814. */
  815. public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
  816. /**
  817. * Starts the list before the elements are added.
  818. *
  819. * @see Walker::start_lvl()
  820. *
  821. * @since 2.1.0
  822. *
  823. * @param string $output Passed by reference. Used to append additional content.
  824. * @param int $depth Depth of category. Used for tab indentation.
  825. * @param array $args An array of arguments. Will only append content if style argument value is 'list'.
  826. * @see wp_list_categories()
  827. */
  828. public function start_lvl( &$output, $depth = 0, $args = array() ) {
  829. if ( 'list' != $args['style'] )
  830. return;
  831. $indent = str_repeat("\t", $depth);
  832. $output .= "$indent<ul class='children'>\n";
  833. }
  834. /**
  835. * Ends the list of after the elements are added.
  836. *
  837. * @see Walker::end_lvl()
  838. *
  839. * @since 2.1.0
  840. *
  841. * @param string $output Passed by reference. Used to append additional content.
  842. * @param int $depth Depth of category. Used for tab indentation.
  843. * @param array $args An array of arguments. Will only append content if style argument value is 'list'.
  844. * @wsee wp_list_categories()
  845. */
  846. public function end_lvl( &$output, $depth = 0, $args = array() ) {
  847. if ( 'list' != $args['style'] )
  848. return;
  849. $indent = str_repeat("\t", $depth);
  850. $output .= "$indent</ul>\n";
  851. }
  852. /**
  853. * Start the element output.
  854. *
  855. * @see Walker::start_el()
  856. *
  857. * @since 2.1.0
  858. *
  859. * @param string $output Passed by reference. Used to append additional content.
  860. * @param object $category Category data object.
  861. * @param int $depth Depth of category in reference to parents. Default 0.
  862. * @param array $args An array of arguments. @see wp_list_categories()
  863. * @param int $id ID of the current category.
  864. */
  865. public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
  866. /** This filter is documented in wp-includes/category-template.php */
  867. $cat_name = apply_filters(
  868. 'list_cats',
  869. esc_attr( $category->name ),
  870. $category
  871. );
  872. $link = '<a href="' . esc_url( get_term_link( $category ) ) . '" ';
  873. if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
  874. /**
  875. * Filter the category description for display.
  876. *
  877. * @since 1.2.0
  878. *
  879. * @param string $description Category description.
  880. * @param object $category Category object.
  881. */
  882. $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
  883. }
  884. $link .= '>';
  885. $link .= $cat_name . '</a>';
  886. if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
  887. $link .= ' ';
  888. if ( empty( $args['feed_image'] ) ) {
  889. $link .= '(';
  890. }
  891. $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"';
  892. if ( empty( $args['feed'] ) ) {
  893. $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
  894. } else {
  895. $alt = ' alt="' . $args['feed'] . '"';
  896. $name = $args['feed'];
  897. $link .= empty( $args['title'] ) ? '' : $args['title'];
  898. }
  899. $link .= '>';
  900. if ( empty( $args['feed_image'] ) ) {
  901. $link .= $name;
  902. } else {
  903. $link .= "<img src='" . $args['feed_image'] . "'$alt" . ' />';
  904. }
  905. $link .= '</a>';
  906. if ( empty( $args['feed_image'] ) ) {
  907. $link .= ')';
  908. }
  909. }
  910. if ( ! empty( $args['show_count'] ) ) {
  911. $link .= ' (' . number_format_i18n( $category->count ) . ')';
  912. }
  913. if ( 'list' == $args['style'] ) {
  914. $output .= "\t<li";
  915. $class = 'cat-item cat-item-' . $category->term_id;
  916. if ( ! empty( $args['current_category'] ) ) {
  917. $_current_category = get_term( $args['current_category'], $category->taxonomy );
  918. if ( $category->term_id == $args['current_category'] ) {
  919. $class .= ' current-cat';
  920. } elseif ( $category->term_id == $_current_category->parent ) {
  921. $class .= ' current-cat-parent';
  922. }
  923. }
  924. $output .= ' class="' . $class . '"';
  925. $output .= ">$link\n";
  926. } else {
  927. $output .= "\t$link<br />\n";
  928. }
  929. }
  930. /**
  931. * Ends the element output, if needed.
  932. *
  933. * @see Walker::end_el()
  934. *
  935. * @since 2.1.0
  936. *
  937. * @param string $output Passed by reference. Used to append additional content.
  938. * @param object $page Not used.
  939. * @param int $depth Depth of category. Not used.
  940. * @param array $args An array of arguments. Only uses 'list' for whether should append to output. @see wp_list_categories()
  941. */
  942. public function end_el( &$output, $page, $depth = 0, $args = array() ) {
  943. if ( 'list' != $args['style'] )
  944. return;
  945. $output .= "</li>\n";
  946. }
  947. }
  948. /**
  949. * Create HTML dropdown list of Categories.
  950. *
  951. * @package WordPress
  952. * @since 2.1.0
  953. * @uses Walker
  954. */
  955. class Walker_CategoryDropdown extends Walker {
  956. /**
  957. * @see Walker::$tree_type
  958. * @since 2.1.0
  959. * @var string
  960. */
  961. public $tree_type = 'category';
  962. /**
  963. * @see Walker::$db_fields
  964. * @since 2.1.0
  965. * @todo Decouple this
  966. * @var array
  967. */
  968. public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
  969. /**
  970. * Start the element output.
  971. *
  972. * @see Walker::start_el()
  973. * @since 2.1.0
  974. *
  975. * @param string $output Passed by reference. Used to append additional content.
  976. * @param object $category Category data object.
  977. * @param int $depth Depth of category. Used for padding.
  978. * @param array $args Uses 'selected' and 'show_count' keys, if they exist. @see wp_dropdown_categories()
  979. */
  980. public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
  981. $pad = str_repeat('&nbsp;', $depth * 3);
  982. /** This filter is documented in wp-includes/category-template.php */
  983. $cat_name = apply_filters( 'list_cats', $category->name, $category );
  984. $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
  985. if ( $category->term_id == $args['selected'] )
  986. $output .= ' selected="selected"';
  987. $output .= '>';
  988. $output .= $pad.$cat_name;
  989. if ( $args['show_count'] )
  990. $output .= '&nbsp;&nbsp;('. number_format_i18n( $category->count ) .')';
  991. $output .= "</option>\n";
  992. }
  993. }
  994. //
  995. // Tags
  996. //
  997. /**
  998. * Retrieve the link to the tag.
  999. *
  1000. * @since 2.3.0
  1001. * @see get_term_link()
  1002. *
  1003. * @param int|object $tag Tag ID or object.
  1004. * @return string Link on success, empty string if tag does not exist.
  1005. */
  1006. function get_tag_link( $tag ) {
  1007. if ( ! is_object( $tag ) )
  1008. $tag = (int) $tag;
  1009. $tag = get_term_link( $tag, 'post_tag' );
  1010. if ( is_wp_error( $tag ) )
  1011. return '';
  1012. return $tag;
  1013. }
  1014. /**
  1015. * Retrieve the tags for a post.
  1016. *
  1017. * @since 2.3.0
  1018. *
  1019. * @param int $id Post ID.
  1020. * @return array|bool Array of tag objects on success, false on failure.
  1021. */
  1022. function get_the_tags( $id = 0 ) {
  1023. /**
  1024. * Filter the array of tags for the given post.
  1025. *
  1026. * @since 2.3.0
  1027. *
  1028. * @see get_the_terms()
  1029. *
  1030. * @param array $terms An array of tags for the given post.
  1031. */
  1032. return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
  1033. }
  1034. /**
  1035. * Retrieve the tags for a post formatted as a string.
  1036. *
  1037. * @since 2.3.0
  1038. *
  1039. * @param string $before Optional. Before tags.
  1040. * @param string $sep Optional. Between tags.
  1041. * @param string $after Optional. After tags.
  1042. * @param int $id Optional. Post ID. Defaults to the current post.
  1043. * @return string|bool|WP_Error A list of tags on success, false if there are no terms, WP_Error on failure.
  1044. */
  1045. function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) {
  1046. /**
  1047. * Filter the tags list for a given post.
  1048. *
  1049. * @since 2.3.0
  1050. *
  1051. * @param string $tag_list List of tags.
  1052. * @param string $before String to use before tags.
  1053. * @param string $sep String to use between the tags.
  1054. * @param string $after String to use after tags.
  1055. * @param int $id Post ID.
  1056. */
  1057. return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id );
  1058. }
  1059. /**
  1060. * Retrieve the tags for a post.
  1061. *
  1062. * @since 2.3.0
  1063. *
  1064. * @param string $before Optional. Before list.
  1065. * @param string $sep Optional. Separate items using this.
  1066. * @param string $after Optional. After list.
  1067. */
  1068. function the_tags( $before = null, $sep = ', ', $after = '' ) {
  1069. if ( null === $before )
  1070. $before = __('Tags: ');
  1071. echo get_the_tag_list($before, $sep, $after);
  1072. }
  1073. /**
  1074. * Retrieve tag description.
  1075. *
  1076. * @since 2.8.0
  1077. *
  1078. * @param int $tag Optional. Tag ID. Will use global tag ID by default.
  1079. * @return string Tag description, available.
  1080. */
  1081. function tag_description( $tag = 0 ) {
  1082. return term_description( $tag );
  1083. }
  1084. /**
  1085. * Retrieve term description.
  1086. *
  1087. * @since 2.8.0
  1088. *
  1089. * @param int $term Optional. Term ID. Will use global term ID by default.
  1090. * @param string $taxonomy Optional taxonomy name. Defaults to 'post_tag'.
  1091. * @return string Term description, available.
  1092. */
  1093. function term_description( $term = 0, $taxonomy = 'post_tag' ) {
  1094. if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) {
  1095. $term = get_queried_object();
  1096. if ( $term ) {
  1097. $taxonomy = $term->taxonomy;
  1098. $term = $term->term_id;
  1099. }
  1100. }
  1101. $description = get_term_field( 'description', $term, $taxonomy );
  1102. return is_wp_error( $description ) ? '' : $description;
  1103. }
  1104. /**
  1105. * Retrieve the terms of the taxonomy that are attached to the post.
  1106. *
  1107. * @since 2.5.0
  1108. *
  1109. * @param int|object $post Post ID or object.
  1110. * @param string $taxonomy Taxonomy name.
  1111. * @return array|bool|WP_Error Array of term objects on success, false if there are no terms
  1112. * or the post does not exist, WP_Error on failure.
  1113. */
  1114. function get_the_terms( $post, $taxonomy ) {
  1115. if ( ! $post = get_post( $post ) )
  1116. return false;
  1117. $terms = get_object_term_cache( $post->ID, $taxonomy );
  1118. if ( false === $terms ) {
  1119. $terms = wp_get_object_terms( $post->ID, $taxonomy );
  1120. wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
  1121. }
  1122. /**
  1123. * Filter the list of terms attached to the given post.
  1124. *
  1125. * @since 3.1.0
  1126. *
  1127. * @param array|WP_Error $terms List of attached terms, or WP_Error on failure.
  1128. * @param int $post_id Post ID.
  1129. * @param string $taxonomy Name of the taxonomy.
  1130. */
  1131. $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );
  1132. if ( empty( $terms ) )
  1133. return false;
  1134. return $terms;
  1135. }
  1136. /**
  1137. * Retrieve a post's terms as a list with specified format.
  1138. *
  1139. * @since 2.5.0
  1140. *
  1141. * @param int $id Post ID.
  1142. * @param string $taxonomy Taxonomy name.
  1143. * @param string $before Optional. Before list.
  1144. * @param string $sep Optional. Separate items using this.
  1145. * @param string $after Optional. After list.
  1146. * @return string|bool|WP_Error A list of terms on success, false if there are no terms, WP_Error on failure.
  1147. */
  1148. function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
  1149. $terms = get_the_terms( $id, $taxonomy );
  1150. if ( is_wp_error( $terms ) )
  1151. return $terms;
  1152. if ( empty( $terms ) )
  1153. return false;
  1154. foreach ( $terms as $term ) {
  1155. $link = get_term_link( $term, $taxonomy );
  1156. if ( is_wp_error( $link ) )
  1157. return $link;
  1158. $term_links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
  1159. }
  1160. /**
  1161. * Filter the term links for a given taxonomy.
  1162. *
  1163. * The dynamic portion of the filter name, `$taxonomy`, refers
  1164. * to the taxonomy slug.
  1165. *
  1166. * @since 2.5.0
  1167. *
  1168. * @param array $term_links An array of term links.
  1169. */
  1170. $term_links = apply_filters( "term_links-$taxonomy", $term_links );
  1171. return $before . join( $sep, $term_links ) . $after;
  1172. }
  1173. /**
  1174. * Display the terms in a list.
  1175. *
  1176. * @since 2.5.0
  1177. *
  1178. * @param int $id Post ID.
  1179. * @param string $taxonomy Taxonomy name.
  1180. * @param string $before Optional. Before list.
  1181. * @param string $sep Optional. Separate items using this.
  1182. * @param string $after Optional. After list.
  1183. * @return false|null False on WordPress error. Returns null when displaying.
  1184. */
  1185. function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
  1186. $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
  1187. if ( is_wp_error( $term_list ) )
  1188. return false;
  1189. /**
  1190. * Filter the list of terms to display.
  1191. *
  1192. * @since 2.9.0
  1193. *
  1194. * @param array $term_list List of terms to display.
  1195. * @param string $taxonomy The taxonomy name.
  1196. * @param string $before String to use before the terms.
  1197. * @param string $sep String to use between the terms.
  1198. * @param string $after String to use after the terms.
  1199. */
  1200. echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after );
  1201. }
  1202. /**
  1203. * Check if the current post has any of given category.
  1204. *
  1205. * @since 3.1.0
  1206. *
  1207. * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for.
  1208. * @param int|object $post Optional. Post to check instead of the current post.
  1209. * @return bool True if the current post has any of the given categories (or any category, if no category specified).
  1210. */
  1211. function has_category( $category = '', $post = null ) {
  1212. return has_term( $category, 'category', $post );
  1213. }
  1214. /**
  1215. * Check if the current post has any of given tags.
  1216. *
  1217. * The given tags are checked against the post's tags' term_ids, names and slugs.
  1218. * Tags given as integers will only be checked against the post's tags' term_ids.
  1219. * If no tags are given, determines if post has any tags.
  1220. *
  1221. * 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)
  1222. * Prior to v2.7, this function could only be used in the WordPress Loop.
  1223. * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
  1224. *
  1225. * @since 2.6.0
  1226. *
  1227. * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
  1228. * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
  1229. * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
  1230. */
  1231. function has_tag( $tag = '', $post = null ) {
  1232. return has_term( $tag, 'post_tag', $post );
  1233. }
  1234. /**
  1235. * Check if the current post has any of given terms.
  1236. *
  1237. * The given terms are checked against the post's terms' term_ids, names and slugs.
  1238. * Terms given as integers will only be checked against the post's terms' term_ids.
  1239. * If no terms are given, determines if post has any terms.
  1240. *
  1241. * @since 3.1.0
  1242. *
  1243. * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
  1244. * @param string $taxonomy Taxonomy name
  1245. * @param int|object $post Optional. Post to check instead of the current post.
  1246. * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
  1247. */
  1248. function has_term( $term = '', $taxonomy = '', $post = null ) {
  1249. $post = get_post($post);
  1250. if ( !$post )
  1251. return false;
  1252. $r = is_object_in_term( $post->ID, $taxonomy, $term );
  1253. if ( is_wp_error( $r ) )
  1254. return false;
  1255. return $r;
  1256. }