PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/category-template.php

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