PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/APP/wp-includes/category-template.php

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