/wp-includes/category-template.php

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