3 * Category Template Tags and API.
10 * Retrieve category link URL.
13 * @see get_term_link()
15 * @param int|object $category Category ID or object.
16 * @return string Link on success, empty string if category does not exist.
18 function get_category_link( $category ) {
19 if ( ! is_object( $category ) )
20 $category = (int) $category;
22 $category = get_term_link( $category, 'category' );
24 if ( is_wp_error( $category ) )
31 * Retrieve category parents with separator.
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.
42 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
44 $parent = &get_category( $id );
45 if ( is_wp_error( $parent ) )
49 $name = $parent->slug;
51 $name = $parent->name;
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 );
59 $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;
61 $chain .= $name.$separator;
66 * Retrieve post categories.
71 * @param int $id Optional, default to current post ID. The post ID.
74 function get_the_category( $id = false ) {
75 $categories = get_the_terms( $id, 'category' );
77 $categories = array();
79 $categories = array_values( $categories );
81 foreach ( array_keys( $categories ) as $key ) {
82 _make_cat_compat( $categories[$key] );
85 // Filter name is plural because we return alot of categories (possibly more than #13237) not just one
86 return apply_filters( 'get_the_categories', $categories );
90 * Sort categories by name.
92 * Used by usort() as a callback, should not be used directly. Can actually be
93 * used to sort any term object.
102 function _usort_terms_by_name( $a, $b ) {
103 return strcmp( $a->name, $b->name );
107 * Sort categories by ID.
109 * Used by usort() as a callback, should not be used directly. Can actually be
110 * used to sort any term object.
119 function _usort_terms_by_ID( $a, $b ) {
120 if ( $a->term_id > $b->term_id )
122 elseif ( $a->term_id < $b->term_id )
129 * Retrieve category name based on category ID.
133 * @param int $cat_ID Category ID.
134 * @return string Category name.
136 function get_the_category_by_ID( $cat_ID ) {
137 $cat_ID = (int) $cat_ID;
138 $category = &get_category( $cat_ID );
139 if ( is_wp_error( $category ) )
141 return $category->name;
145 * Retrieve category list in either HTML list or custom format.
149 * @param string $separator Optional, default is empty string. Separator for between the categories.
150 * @param string $parents Optional. How to display the parents.
151 * @param int $post_id Optional. Post ID to retrieve categories.
154 function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
156 if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) )
157 return apply_filters( 'the_category', '', $separator, $parents );
159 $categories = get_the_category( $post_id );
160 if ( empty( $categories ) )
161 return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
163 $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
166 if ( '' == $separator ) {
167 $thelist .= '<ul class="post-categories">';
168 foreach ( $categories as $category ) {
169 $thelist .= "\n\t<li>";
170 switch ( strtolower( $parents ) ) {
172 if ( $category->parent )
173 $thelist .= get_category_parents( $category->parent, true, $separator );
174 $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>';
177 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
178 if ( $category->parent )
179 $thelist .= get_category_parents( $category->parent, false, $separator );
180 $thelist .= $category->name.'</a></li>';
184 $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>';
190 foreach ( $categories as $category ) {
192 $thelist .= $separator;
193 switch ( strtolower( $parents ) ) {
195 if ( $category->parent )
196 $thelist .= get_category_parents( $category->parent, true, $separator );
197 $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>';
200 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
201 if ( $category->parent )
202 $thelist .= get_category_parents( $category->parent, false, $separator );
203 $thelist .= "$category->name</a>";
207 $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>';
212 return apply_filters( 'the_category', $thelist, $separator, $parents );
216 * Check if the current post in within any of the given categories.
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.
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.
229 * @param int|string|array $category Category ID, name or slug, or array of said.
230 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
231 * @return bool True if the current post is in any of the given categories.
233 function in_category( $category, $post = null ) {
234 if ( empty( $category ) )
237 return has_term( $category, 'category', $post );
241 * Display the category list for the post.
245 * @param string $separator Optional, default is empty string. Separator for between the categories.
246 * @param string $parents Optional. How to display the parents.
247 * @param int $post_id Optional. Post ID to retrieve categories.
249 function the_category( $separator = '', $parents='', $post_id = false ) {
250 echo get_the_category_list( $separator, $parents, $post_id );
254 * Retrieve category description.
258 * @param int $category Optional. Category ID. Will use global category ID by default.
259 * @return string Category description, available.
261 function category_description( $category = 0 ) {
262 return term_description( $category, 'category' );
266 * Display or retrieve the HTML dropdown list of categories.
268 * The list of arguments is below:
269 * 'show_option_all' (string) - Text to display for showing all categories.
270 * 'show_option_none' (string) - Text to display for showing no categories.
271 * 'orderby' (string) default is 'ID' - What column to use for ordering the
273 * 'order' (string) default is 'ASC' - What direction to order categories.
274 * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
276 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
277 * don't have any posts attached to them.
278 * 'child_of' (int) default is 0 - See {@link get_categories()}.
279 * 'exclude' (string) - See {@link get_categories()}.
280 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
281 * 'depth' (int) - The max depth.
282 * 'tab_index' (int) - Tab index for select element.
283 * 'name' (string) - The name attribute value for select element.
284 * 'id' (string) - The ID attribute value for select element. Defaults to name if omitted.
285 * 'class' (string) - The class attribute value for select element.
286 * 'selected' (int) - Which category ID is selected.
287 * 'taxonomy' (string) - The name of the taxonomy to retrieve. Defaults to category.
289 * The 'hierarchical' argument, which is disabled by default, will override the
290 * depth argument, unless it is true. When the argument is false, it will
291 * display all of the categories. When it is enabled it will use the value in
292 * the 'depth' argument.
296 * @param string|array $args Optional. Override default arguments.
297 * @return string HTML content only if 'echo' argument is 0.
299 function wp_dropdown_categories( $args = '' ) {
301 'show_option_all' => '', 'show_option_none' => '',
302 'orderby' => 'id', 'order' => 'ASC',
304 'hide_empty' => 1, 'child_of' => 0,
305 'exclude' => '', 'echo' => 1,
306 'selected' => 0, 'hierarchical' => 0,
307 'name' => 'cat', 'id' => '',
308 'class' => 'postform', 'depth' => 0,
309 'tab_index' => 0, 'taxonomy' => 'category',
310 'hide_if_empty' => false
313 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
316 if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
317 _deprecated_argument( __FUNCTION__, '3.0', '' );
318 $args['taxonomy'] = 'link_category';
321 $r = wp_parse_args( $args, $defaults );
323 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
324 $r['pad_counts'] = true;
329 $tab_index_attribute = '';
330 if ( (int) $tab_index > 0 )
331 $tab_index_attribute = " tabindex=\"$tab_index\"";
333 $categories = get_terms( $taxonomy, $r );
334 $name = esc_attr( $name );
335 $class = esc_attr( $class );
336 $id = $id ? esc_attr( $id ) : $name;
338 if ( ! $r['hide_if_empty'] || ! empty($categories) )
339 $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
343 if ( empty($categories) && ! $r['hide_if_empty'] && !empty($show_option_none) ) {
344 $show_option_none = apply_filters( 'list_cats', $show_option_none );
345 $output .= "\t<option value='-1' selected='selected'>$show_option_none</option>\n";
348 if ( ! empty( $categories ) ) {
350 if ( $show_option_all ) {
351 $show_option_all = apply_filters( 'list_cats', $show_option_all );
352 $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
353 $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
356 if ( $show_option_none ) {
357 $show_option_none = apply_filters( 'list_cats', $show_option_none );
358 $selected = ( '-1' === strval($r['selected']) ) ? " selected='selected'" : '';
359 $output .= "\t<option value='-1'$selected>$show_option_none</option>\n";
363 $depth = $r['depth']; // Walk the full depth.
365 $depth = -1; // Flat.
367 $output .= walk_category_dropdown_tree( $categories, $depth, $r );
370 if ( ! $r['hide_if_empty'] || ! empty($categories) )
371 $output .= "</select>\n";
373 $output = apply_filters( 'wp_dropdown_cats', $output );
382 * Display or retrieve the HTML list of categories.
384 * The list of arguments is below:
385 * 'show_option_all' (string) - Text to display for showing all categories.
386 * 'orderby' (string) default is 'ID' - What column to use for ordering the
388 * 'order' (string) default is 'ASC' - What direction to order categories.
389 * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
391 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
392 * don't have any posts attached to them.
393 * 'use_desc_for_title' (bool|int) default is 1 - Whether to use the
394 * description instead of the category title.
395 * 'feed' - See {@link get_categories()}.
396 * 'feed_type' - See {@link get_categories()}.
397 * 'feed_image' - See {@link get_categories()}.
398 * 'child_of' (int) default is 0 - See {@link get_categories()}.
399 * 'exclude' (string) - See {@link get_categories()}.
400 * 'exclude_tree' (string) - See {@link get_categories()}.
401 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
402 * 'current_category' (int) - See {@link get_categories()}.
403 * 'hierarchical' (bool) - See {@link get_categories()}.
404 * 'title_li' (string) - See {@link get_categories()}.
405 * 'depth' (int) - The max depth.
409 * @param string|array $args Optional. Override default arguments.
410 * @return string HTML content only if 'echo' argument is 0.
412 function wp_list_categories( $args = '' ) {
414 'show_option_all' => '', 'show_option_none' => __('No categories'),
415 'orderby' => 'name', 'order' => 'ASC',
417 'show_count' => 0, 'hide_empty' => 1,
418 'use_desc_for_title' => 1, 'child_of' => 0,
419 'feed' => '', 'feed_type' => '',
420 'feed_image' => '', 'exclude' => '',
421 'exclude_tree' => '', 'current_category' => 0,
422 'hierarchical' => true, 'title_li' => __( 'Categories' ),
423 'echo' => 1, 'depth' => 0,
424 'taxonomy' => 'category'
427 $r = wp_parse_args( $args, $defaults );
429 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
430 $r['pad_counts'] = true;
432 if ( true == $r['hierarchical'] ) {
433 $r['exclude_tree'] = $r['exclude'];
437 if ( !isset( $r['class'] ) )
438 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
442 if ( !taxonomy_exists($taxonomy) )
445 $categories = get_categories( $r );
448 if ( $title_li && 'list' == $style )
449 $output = '<li class="' . esc_attr( $class ) . '">' . $title_li . '<ul>';
451 if ( empty( $categories ) ) {
452 if ( ! empty( $show_option_none ) ) {
453 if ( 'list' == $style )
454 $output .= '<li>' . $show_option_none . '</li>';
456 $output .= $show_option_none;
459 if ( ! empty( $show_option_all ) ) {
460 $posts_page = ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) ? get_permalink( get_option( 'page_for_posts' ) ) : home_url( '/' );
461 $posts_page = esc_url( $posts_page );
462 if ( 'list' == $style )
463 $output .= "<li><a href='$posts_page'>$show_option_all</a></li>";
465 $output .= "<a href='$posts_page'>$show_option_all</a>";
468 if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
469 $current_term_object = get_queried_object();
470 if ( $r['taxonomy'] == $current_term_object->taxonomy )
471 $r['current_category'] = get_queried_object_id();
475 $depth = $r['depth'];
477 $depth = -1; // Flat.
479 $output .= walk_category_tree( $categories, $depth, $r );
482 if ( $title_li && 'list' == $style )
483 $output .= '</ul></li>';
485 $output = apply_filters( 'wp_list_categories', $output, $args );
496 * The text size is set by the 'smallest' and 'largest' arguments, which will
497 * use the 'unit' argument value for the CSS text size unit. The 'format'
498 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
499 * 'format' argument will separate tags with spaces. The list value for the
500 * 'format' argument will format the tags in a UL HTML list. The array value for
501 * the 'format' argument will return in PHP array type format.
503 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
504 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
506 * The 'number' argument is how many tags to return. By default, the limit will
507 * be to return the top 45 tags in the tag cloud list.
509 * The 'topic_count_text_callback' argument is a function, which, given the count
510 * of the posts with that tag, returns a text for the tooltip of the tag link.
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.
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.
521 function wp_tag_cloud( $args = '' ) {
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', 'echo' => true
527 $args = wp_parse_args( $args, $defaults );
529 $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
531 if ( empty( $tags ) || is_wp_error( $tags ) )
534 foreach ( $tags as $key => $tag ) {
535 if ( 'edit' == $args['link'] )
536 $link = get_edit_tag_link( $tag->term_id, $tag->taxonomy );
538 $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
539 if ( is_wp_error( $link ) )
542 $tags[ $key ]->link = $link;
543 $tags[ $key ]->id = $tag->term_id;
546 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
548 $return = apply_filters( 'wp_tag_cloud', $return, $args );
550 if ( 'array' == $args['format'] || empty($args['echo']) )
557 * Default text for tooltip for tag links
559 * @param integer $count number of posts with that tag
560 * @return string text for the tooltip of a tag link.
562 function default_topic_count_text( $count ) {
563 return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) );
567 * Default topic count scaling for tag links
569 * @param integer $count number of posts with that tag
570 * @return integer scaled count
572 function default_topic_count_scale( $count ) {
573 return round(log10($count + 1) * 100);
577 * Generates a tag cloud (heatmap) from provided data.
579 * The text size is set by the 'smallest' and 'largest' arguments, which will
580 * use the 'unit' argument value for the CSS text size unit. The 'format'
581 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
582 * 'format' argument will separate tags with spaces. The list value for the
583 * 'format' argument will format the tags in a UL HTML list. The array value for
584 * the 'format' argument will return in PHP array type format.
586 * The 'tag_cloud_sort' filter allows you to override the sorting.
587 * Passed to the filter: $tags array and $args array, has to return the $tags array
590 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
591 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
594 * The 'number' argument is how many tags to return. By default, the limit will
595 * be to return the entire tag cloud list.
597 * The 'topic_count_text_callback' argument is a function, which given the count
598 * of the posts with that tag returns a text for the tooltip of the tag link.
600 * @todo Complete functionality.
603 * @param array $tags List of tags.
604 * @param string|array $args Optional, override default arguments.
607 function wp_generate_tag_cloud( $tags, $args = '' ) {
609 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
610 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
611 'topic_count_text_callback' => 'default_topic_count_text',
612 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
615 if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
616 $body = 'return sprintf (
617 _n(' . var_export($args['single_text'], true) . ', ' . var_export($args['multiple_text'], true) . ', $count),
618 number_format_i18n( $count ));';
619 $args['topic_count_text_callback'] = create_function('$count', $body);
622 $args = wp_parse_args( $args, $defaults );
625 if ( empty( $tags ) )
628 $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
629 if ( $tags_sorted != $tags ) { // the tags have been sorted by a plugin
630 $tags = $tags_sorted;
633 if ( 'RAND' == $order ) {
636 // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
637 if ( 'name' == $orderby )
638 uasort( $tags, '_wp_object_name_sort_cb' );
640 uasort( $tags, '_wp_object_count_sort_cb' );
642 if ( 'DESC' == $order )
643 $tags = array_reverse( $tags, true );
648 $tags = array_slice($tags, 0, $number);
651 $real_counts = array(); // For the alt tag
652 foreach ( (array) $tags as $key => $tag ) {
653 $real_counts[ $key ] = $tag->count;
654 $counts[ $key ] = $topic_count_scale_callback($tag->count);
657 $min_count = min( $counts );
658 $spread = max( $counts ) - $min_count;
661 $font_spread = $largest - $smallest;
662 if ( $font_spread < 0 )
664 $font_step = $font_spread / $spread;
668 foreach ( $tags as $key => $tag ) {
669 $count = $counts[ $key ];
670 $real_count = $real_counts[ $key ];
671 $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
672 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
673 $tag_name = $tags[ $key ]->name;
674 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( call_user_func( $topic_count_text_callback, $real_count ) ) . "' style='font-size: " .
675 str_replace( ',', '.', ( $smallest + ( ( $count - $min_count ) * $font_step ) ) )
676 . "$unit;'>$tag_name</a>";
684 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
685 $return .= join( "</li>\n\t<li>", $a );
686 $return .= "</li>\n</ul>\n";
689 $return = join( $separator, $a );
694 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
700 * Callback for comparing objects based on name
705 function _wp_object_name_sort_cb( $a, $b ) {
706 return strnatcasecmp( $a->name, $b->name );
710 * Callback for comparing objects based on count
715 function _wp_object_count_sort_cb( $a, $b ) {
716 return ( $a->count > $b->count );
724 * Retrieve HTML list content for category list.
726 * @uses Walker_Category to create HTML list content.
728 * @see Walker_Category::walk() for parameters and return description.
730 function walk_category_tree() {
731 $args = func_get_args();
732 // the user's options are the third parameter
733 if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
734 $walker = new Walker_Category;
736 $walker = $args[2]['walker'];
738 return call_user_func_array(array( &$walker, 'walk' ), $args );
742 * Retrieve HTML dropdown (select) content for category list.
744 * @uses Walker_CategoryDropdown to create HTML dropdown content.
746 * @see Walker_CategoryDropdown::walk() for parameters and return description.
748 function walk_category_dropdown_tree() {
749 $args = func_get_args();
750 // the user's options are the third parameter
751 if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
752 $walker = new Walker_CategoryDropdown;
754 $walker = $args[2]['walker'];
756 return call_user_func_array(array( &$walker, 'walk' ), $args );
760 * Create HTML list of categories.
766 class Walker_Category extends Walker {
768 * @see Walker::$tree_type
772 var $tree_type = 'category';
775 * @see Walker::$db_fields
777 * @todo Decouple this
780 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
783 * @see Walker::start_lvl()
786 * @param string $output Passed by reference. Used to append additional content.
787 * @param int $depth Depth of category. Used for tab indentation.
788 * @param array $args Will only append content if style argument value is 'list'.
790 function start_lvl( &$output, $depth = 0, $args = array() ) {
791 if ( 'list' != $args['style'] )
794 $indent = str_repeat("\t", $depth);
795 $output .= "$indent<ul class='children'>\n";
799 * @see Walker::end_lvl()
802 * @param string $output Passed by reference. Used to append additional content.
803 * @param int $depth Depth of category. Used for tab indentation.
804 * @param array $args Will only append content if style argument value is 'list'.
806 function end_lvl( &$output, $depth = 0, $args = array() ) {
807 if ( 'list' != $args['style'] )
810 $indent = str_repeat("\t", $depth);
811 $output .= "$indent</ul>\n";
815 * @see Walker::start_el()
818 * @param string $output Passed by reference. Used to append additional content.
819 * @param object $category Category data object.
820 * @param int $depth Depth of category in reference to parents.
823 function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
826 $cat_name = esc_attr( $category->name );
827 $cat_name = apply_filters( 'list_cats', $cat_name, $category );
828 $link = '<a href="' . esc_url( get_term_link($category) ) . '" ';
829 if ( $use_desc_for_title == 0 || empty($category->description) )
830 $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
832 $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
834 $link .= $cat_name . '</a>';
836 if ( !empty($feed_image) || !empty($feed) ) {
839 if ( empty($feed_image) )
842 $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) ) . '"';
844 if ( empty($feed) ) {
845 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
847 $title = ' title="' . $feed . '"';
848 $alt = ' alt="' . $feed . '"';
855 if ( empty($feed_image) )
858 $link .= "<img src='$feed_image'$alt$title" . ' />';
862 if ( empty($feed_image) )
866 if ( !empty($show_count) )
867 $link .= ' (' . intval($category->count) . ')';
869 if ( 'list' == $args['style'] ) {
871 $class = 'cat-item cat-item-' . $category->term_id;
872 if ( !empty($current_category) ) {
873 $_current_category = get_term( $current_category, $category->taxonomy );
874 if ( $category->term_id == $current_category )
875 $class .= ' current-cat';
876 elseif ( $category->term_id == $_current_category->parent )
877 $class .= ' current-cat-parent';
879 $output .= ' class="' . $class . '"';
880 $output .= ">$link\n";
882 $output .= "\t$link<br />\n";
887 * @see Walker::end_el()
890 * @param string $output Passed by reference. Used to append additional content.
891 * @param object $page Not used.
892 * @param int $depth Depth of category. Not used.
893 * @param array $args Only uses 'list' for whether should append to output.
895 function end_el( &$output, $page, $depth = 0, $args = array() ) {
896 if ( 'list' != $args['style'] )
899 $output .= "</li>\n";
905 * Create HTML dropdown list of Categories.
911 class Walker_CategoryDropdown extends Walker {
913 * @see Walker::$tree_type
917 var $tree_type = 'category';
920 * @see Walker::$db_fields
922 * @todo Decouple this
925 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
928 * @see Walker::start_el()
931 * @param string $output Passed by reference. Used to append additional content.
932 * @param object $category Category data object.
933 * @param int $depth Depth of category. Used for padding.
934 * @param array $args Uses 'selected' and 'show_count' keys, if they exist.
936 function start_el( &$output, $category, $depth, $args, $id = 0 ) {
937 $pad = str_repeat(' ', $depth * 3);
939 $cat_name = apply_filters('list_cats', $category->name, $category);
940 $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
941 if ( $category->term_id == $args['selected'] )
942 $output .= ' selected="selected"';
944 $output .= $pad.$cat_name;
945 if ( $args['show_count'] )
946 $output .= ' ('. $category->count .')';
947 $output .= "</option>\n";
956 * Retrieve the link to the tag.
959 * @see get_term_link()
961 * @param int|object $tag Tag ID or object.
962 * @return string Link on success, empty string if tag does not exist.
964 function get_tag_link( $tag ) {
965 if ( ! is_object( $tag ) )
968 $tag = get_term_link( $tag, 'post_tag' );
970 if ( is_wp_error( $tag ) )
977 * Retrieve the tags for a post.
980 * @uses apply_filters() Calls 'get_the_tags' filter on the list of post tags.
982 * @param int $id Post ID.
985 function get_the_tags( $id = 0 ) {
986 return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
990 * Retrieve the tags for a post formatted as a string.
993 * @uses apply_filters() Calls 'the_tags' filter on string list of tags.
995 * @param string $before Optional. Before tags.
996 * @param string $sep Optional. Between tags.
997 * @param string $after Optional. After tags.
998 * @param int $id Optional. Post ID. Defaults to the current post.
1001 function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) {
1002 return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id );
1006 * Retrieve the tags for a post.
1010 * @param string $before Optional. Before list.
1011 * @param string $sep Optional. Separate items using this.
1012 * @param string $after Optional. After list.
1015 function the_tags( $before = null, $sep = ', ', $after = '' ) {
1016 if ( null === $before )
1017 $before = __('Tags: ');
1018 echo get_the_tag_list($before, $sep, $after);
1022 * Retrieve tag description.
1026 * @param int $tag Optional. Tag ID. Will use global tag ID by default.
1027 * @return string Tag description, available.
1029 function tag_description( $tag = 0 ) {
1030 return term_description( $tag );
1034 * Retrieve term description.
1038 * @param int $term Optional. Term ID. Will use global term ID by default.
1039 * @param string $taxonomy Optional taxonomy name. Defaults to 'post_tag'.
1040 * @return string Term description, available.
1042 function term_description( $term = 0, $taxonomy = 'post_tag' ) {
1043 if ( !$term && ( is_tax() || is_tag() || is_category() ) ) {
1044 $term = get_queried_object();
1045 $taxonomy = $term->taxonomy;
1046 $term = $term->term_id;
1048 $description = get_term_field( 'description', $term, $taxonomy );
1049 return is_wp_error( $description ) ? '' : $description;
1053 * Retrieve the terms of the taxonomy that are attached to the post.
1057 * @param int $id Post ID.
1058 * @param string $taxonomy Taxonomy name.
1059 * @return array|bool False on failure. Array of term objects on success.
1061 function get_the_terms( $id, $taxonomy ) {
1067 if ( empty( $post->ID ) )
1070 $id = (int) $post->ID;
1073 $terms = get_object_term_cache( $id, $taxonomy );
1074 if ( false === $terms ) {
1075 $terms = wp_get_object_terms( $id, $taxonomy );
1076 wp_cache_add($id, $terms, $taxonomy . '_relationships');
1079 $terms = apply_filters( 'get_the_terms', $terms, $id, $taxonomy );
1081 if ( empty( $terms ) )
1088 * Retrieve a post's terms as a list with specified format.
1092 * @param int $id Post ID.
1093 * @param string $taxonomy Taxonomy name.
1094 * @param string $before Optional. Before list.
1095 * @param string $sep Optional. Separate items using this.
1096 * @param string $after Optional. After list.
1099 function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
1100 $terms = get_the_terms( $id, $taxonomy );
1102 if ( is_wp_error( $terms ) )
1105 if ( empty( $terms ) )
1108 foreach ( $terms as $term ) {
1109 $link = get_term_link( $term, $taxonomy );
1110 if ( is_wp_error( $link ) )
1112 $term_links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
1115 $term_links = apply_filters( "term_links-$taxonomy", $term_links );
1117 return $before . join( $sep, $term_links ) . $after;
1121 * Display the terms in a list.
1125 * @param int $id Post ID.
1126 * @param string $taxonomy Taxonomy name.
1127 * @param string $before Optional. Before list.
1128 * @param string $sep Optional. Separate items using this.
1129 * @param string $after Optional. After list.
1130 * @return null|bool False on WordPress error. Returns null when displaying.
1132 function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
1133 $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
1135 if ( is_wp_error( $term_list ) )
1138 echo apply_filters('the_terms', $term_list, $taxonomy, $before, $sep, $after);
1142 * Check if the current post has any of given category.
1146 * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for.
1147 * @param int|object $post Optional. Post to check instead of the current post.
1148 * @return bool True if the current post has any of the given categories (or any category, if no category specified).
1150 function has_category( $category = '', $post = null ) {
1151 return has_term( $category, 'category', $post );
1155 * Check if the current post has any of given tags.
1157 * The given tags are checked against the post's tags' term_ids, names and slugs.
1158 * Tags given as integers will only be checked against the post's tags' term_ids.
1159 * If no tags are given, determines if post has any tags.
1161 * 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)
1162 * Prior to v2.7, this function could only be used in the WordPress Loop.
1163 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
1167 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
1168 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
1169 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1171 function has_tag( $tag = '', $post = null ) {
1172 return has_term( $tag, 'post_tag', $post );
1176 * Check if the current post has any of given terms.
1178 * The given terms are checked against the post's terms' term_ids, names and slugs.
1179 * Terms given as integers will only be checked against the post's terms' term_ids.
1180 * If no terms are given, determines if post has any terms.
1184 * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
1185 * @param string $taxonomy Taxonomy name
1186 * @param int|object $post Optional. Post to check instead of the current post.
1187 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1189 function has_term( $term = '', $taxonomy = '', $post = null ) {
1190 $post = get_post($post);
1195 $r = is_object_in_term( $post->ID, $taxonomy, $term );
1196 if ( is_wp_error( $r ) )