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="' . 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 $categories = get_the_category( $post_id );
157 if ( !is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) )
158 return apply_filters( 'the_category', '', $separator, $parents );
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="' . 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="' . 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="' . 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="' . 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="' . 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="' . 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 );
217 * Check if the current post in within any of the given categories.
219 * The given categories are checked against the post's categories' term_ids, names and slugs.
220 * Categories given as integers will only be checked against the post's categories' term_ids.
222 * Prior to v2.5 of WordPress, category names were not supported.
223 * Prior to v2.7, category slugs were not supported.
224 * Prior to v2.7, only one category could be compared: in_category( $single_category ).
225 * Prior to v2.7, this function could only be used in the WordPress Loop.
226 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
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.
234 function in_category( $category, $post = null ) {
235 if ( empty( $category ) )
238 return has_term( $category, 'category', $post );
242 * Display the category list for the post.
246 * @param string $separator Optional, default is empty string. Separator for between the categories.
247 * @param string $parents Optional. How to display the parents.
248 * @param int $post_id Optional. Post ID to retrieve categories.
250 function the_category( $separator = '', $parents='', $post_id = false ) {
251 echo get_the_category_list( $separator, $parents, $post_id );
255 * Retrieve category description.
259 * @param int $category Optional. Category ID. Will use global category ID by default.
260 * @return string Category description, available.
262 function category_description( $category = 0 ) {
263 return term_description( $category, 'category' );
267 * Display or retrieve the HTML dropdown list of categories.
269 * The list of arguments is below:
270 * 'show_option_all' (string) - Text to display for showing all categories.
271 * 'show_option_none' (string) - Text to display for showing no categories.
272 * 'orderby' (string) default is 'ID' - What column to use for ordering the
274 * 'order' (string) default is 'ASC' - What direction to order categories.
275 * 'show_last_update' (bool|int) default is 0 - See {@link get_categories()}
276 * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
278 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
279 * don't have any posts attached to them.
280 * 'child_of' (int) default is 0 - See {@link get_categories()}.
281 * 'exclude' (string) - See {@link get_categories()}.
282 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
283 * 'depth' (int) - The max depth.
284 * 'tab_index' (int) - Tab index for select element.
285 * 'name' (string) - The name attribute value for select element.
286 * 'id' (string) - The ID attribute value for select element. Defaults to name if omitted.
287 * 'class' (string) - The class attribute value for select element.
288 * 'selected' (int) - Which category ID is selected.
289 * 'taxonomy' (string) - The name of the taxonomy to retrieve. Defaults to category.
291 * The 'hierarchical' argument, which is disabled by default, will override the
292 * depth argument, unless it is true. When the argument is false, it will
293 * display all of the categories. When it is enabled it will use the value in
294 * the 'depth' argument.
298 * @param string|array $args Optional. Override default arguments.
299 * @return string HTML content only if 'echo' argument is 0.
301 function wp_dropdown_categories( $args = '' ) {
303 'show_option_all' => '', 'show_option_none' => '',
304 'orderby' => 'id', 'order' => 'ASC',
305 'show_last_update' => 0, 'show_count' => 0,
306 'hide_empty' => 1, 'child_of' => 0,
307 'exclude' => '', 'echo' => 1,
308 'selected' => 0, 'hierarchical' => 0,
309 'name' => 'cat', 'id' => '',
310 'class' => 'postform', 'depth' => 0,
311 'tab_index' => 0, 'taxonomy' => 'category',
312 'hide_if_empty' => false
315 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
318 if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
319 _deprecated_argument( __FUNCTION__, '3.0', '' );
320 $args['taxonomy'] = 'link_category';
323 $r = wp_parse_args( $args, $defaults );
325 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
326 $r['pad_counts'] = true;
329 $r['include_last_update_time'] = $r['show_last_update'];
332 $tab_index_attribute = '';
333 if ( (int) $tab_index > 0 )
334 $tab_index_attribute = " tabindex=\"$tab_index\"";
336 $categories = get_terms( $taxonomy, $r );
337 $name = esc_attr( $name );
338 $class = esc_attr( $class );
339 $id = $id ? esc_attr( $id ) : $name;
341 if ( ! $r['hide_if_empty'] || ! empty($categories) )
342 $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
346 if ( empty($categories) && ! $r['hide_if_empty'] && !empty($show_option_none) ) {
347 $show_option_none = apply_filters( 'list_cats', $show_option_none );
348 $output .= "\t<option value='-1' selected='selected'>$show_option_none</option>\n";
351 if ( ! empty( $categories ) ) {
353 if ( $show_option_all ) {
354 $show_option_all = apply_filters( 'list_cats', $show_option_all );
355 $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
356 $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
359 if ( $show_option_none ) {
360 $show_option_none = apply_filters( 'list_cats', $show_option_none );
361 $selected = ( '-1' === strval($r['selected']) ) ? " selected='selected'" : '';
362 $output .= "\t<option value='-1'$selected>$show_option_none</option>\n";
366 $depth = $r['depth']; // Walk the full depth.
368 $depth = -1; // Flat.
370 $output .= walk_category_dropdown_tree( $categories, $depth, $r );
372 if ( ! $r['hide_if_empty'] || ! empty($categories) )
373 $output .= "</select>\n";
376 $output = apply_filters( 'wp_dropdown_cats', $output );
385 * Display or retrieve the HTML list of categories.
387 * The list of arguments is below:
388 * 'show_option_all' (string) - Text to display for showing all categories.
389 * 'orderby' (string) default is 'ID' - What column to use for ordering the
391 * 'order' (string) default is 'ASC' - What direction to order categories.
392 * 'show_last_update' (bool|int) default is 0 - See {@link
393 * walk_category_dropdown_tree()}
394 * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
396 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
397 * don't have any posts attached to them.
398 * 'use_desc_for_title' (bool|int) default is 1 - Whether to use the
399 * description instead of the category title.
400 * 'feed' - See {@link get_categories()}.
401 * 'feed_type' - See {@link get_categories()}.
402 * 'feed_image' - See {@link get_categories()}.
403 * 'child_of' (int) default is 0 - See {@link get_categories()}.
404 * 'exclude' (string) - See {@link get_categories()}.
405 * 'exclude_tree' (string) - See {@link get_categories()}.
406 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
407 * 'current_category' (int) - See {@link get_categories()}.
408 * 'hierarchical' (bool) - See {@link get_categories()}.
409 * 'title_li' (string) - See {@link get_categories()}.
410 * 'depth' (int) - The max depth.
414 * @param string|array $args Optional. Override default arguments.
415 * @return string HTML content only if 'echo' argument is 0.
417 function wp_list_categories( $args = '' ) {
419 'show_option_all' => '', 'show_option_none' => __('No categories'),
420 'orderby' => 'name', 'order' => 'ASC',
421 'show_last_update' => 0, 'style' => 'list',
422 'show_count' => 0, 'hide_empty' => 1,
423 'use_desc_for_title' => 1, 'child_of' => 0,
424 'feed' => '', 'feed_type' => '',
425 'feed_image' => '', 'exclude' => '',
426 'exclude_tree' => '', 'current_category' => 0,
427 'hierarchical' => true, 'title_li' => __( 'Categories' ),
428 'echo' => 1, 'depth' => 0,
429 'taxonomy' => 'category'
432 $r = wp_parse_args( $args, $defaults );
434 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
435 $r['pad_counts'] = true;
437 if ( isset( $r['show_date'] ) )
438 $r['include_last_update_time'] = $r['show_date'];
440 if ( true == $r['hierarchical'] ) {
441 $r['exclude_tree'] = $r['exclude'];
445 if ( !isset( $r['class'] ) )
446 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
450 if ( !taxonomy_exists($taxonomy) )
453 $categories = get_categories( $r );
456 if ( $title_li && 'list' == $style )
457 $output = '<li class="' . esc_attr( $class ) . '">' . $title_li . '<ul>';
459 if ( empty( $categories ) ) {
460 if ( ! empty( $show_option_none ) ) {
461 if ( 'list' == $style )
462 $output .= '<li>' . $show_option_none . '</li>';
464 $output .= $show_option_none;
467 if( !empty( $show_option_all ) )
468 if ( 'list' == $style )
469 $output .= '<li><a href="' . get_bloginfo( 'url' ) . '">' . $show_option_all . '</a></li>';
471 $output .= '<a href="' . get_bloginfo( 'url' ) . '">' . $show_option_all . '</a>';
473 if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
474 $current_term_object = get_queried_object();
475 if ( $r['taxonomy'] == $current_term_object->taxonomy )
476 $r['current_category'] = get_queried_object_id();
480 $depth = $r['depth'];
482 $depth = -1; // Flat.
484 $output .= walk_category_tree( $categories, $depth, $r );
487 if ( $title_li && 'list' == $style )
488 $output .= '</ul></li>';
490 $output = apply_filters( 'wp_list_categories', $output, $args );
501 * The text size is set by the 'smallest' and 'largest' arguments, which will
502 * use the 'unit' argument value for the CSS text size unit. The 'format'
503 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
504 * 'format' argument will separate tags with spaces. The list value for the
505 * 'format' argument will format the tags in a UL HTML list. The array value for
506 * the 'format' argument will return in PHP array type format.
508 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
509 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
511 * The 'number' argument is how many tags to return. By default, the limit will
512 * be to return the top 45 tags in the tag cloud list.
514 * The 'topic_count_text_callback' argument is a function, which, given the count
515 * of the posts with that tag, returns a text for the tooltip of the tag link.
517 * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
518 * function. Only one should be used, because only one will be used and the
519 * other ignored, if they are both set.
523 * @param array|string $args Optional. Override default arguments.
524 * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
526 function wp_tag_cloud( $args = '' ) {
528 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
529 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
530 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
532 $args = wp_parse_args( $args, $defaults );
534 $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
536 if ( empty( $tags ) || is_wp_error( $tags ) )
539 foreach ( $tags as $key => $tag ) {
540 if ( 'edit' == $args['link'] )
541 $link = get_edit_tag_link( $tag->term_id, $tag->taxonomy );
543 $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
544 if ( is_wp_error( $link ) )
547 $tags[ $key ]->link = $link;
548 $tags[ $key ]->id = $tag->term_id;
551 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
553 $return = apply_filters( 'wp_tag_cloud', $return, $args );
555 if ( 'array' == $args['format'] || empty($args['echo']) )
562 * Default text for tooltip for tag links
564 * @param integer $count number of posts with that tag
565 * @return string text for the tooltip of a tag link.
567 function default_topic_count_text( $count ) {
568 return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) );
572 * Default topic count scaling for tag links
574 * @param integer $count number of posts with that tag
575 * @return integer scaled count
577 function default_topic_count_scale( $count ) {
578 return round(log10($count + 1) * 100);
583 * Generates a tag cloud (heatmap) from provided data.
585 * The text size is set by the 'smallest' and 'largest' arguments, which will
586 * use the 'unit' argument value for the CSS text size unit. The 'format'
587 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
588 * 'format' argument will separate tags with spaces. The list value for the
589 * 'format' argument will format the tags in a UL HTML list. The array value for
590 * the 'format' argument will return in PHP array type format.
592 * The 'tag_cloud_sort' filter allows you to override the sorting.
593 * Passed to the filter: $tags array and $args array, has to return the $tags array
596 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
597 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
600 * The 'number' argument is how many tags to return. By default, the limit will
601 * be to return the entire tag cloud list.
603 * The 'topic_count_text_callback' argument is a function, which given the count
604 * of the posts with that tag returns a text for the tooltip of the tag link.
606 * @todo Complete functionality.
609 * @param array $tags List of tags.
610 * @param string|array $args Optional, override default arguments.
613 function wp_generate_tag_cloud( $tags, $args = '' ) {
616 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
617 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
618 'topic_count_text_callback' => 'default_topic_count_text',
619 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
622 if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
623 $body = 'return sprintf (
624 _n(' . var_export($args['single_text'], true) . ', ' . var_export($args['multiple_text'], true) . ', $count),
625 number_format_i18n( $count ));';
626 $args['topic_count_text_callback'] = create_function('$count', $body);
629 $args = wp_parse_args( $args, $defaults );
632 if ( empty( $tags ) )
635 $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
636 if ( $tags_sorted != $tags ) { // the tags have been sorted by a plugin
637 $tags = $tags_sorted;
640 if ( 'RAND' == $order ) {
643 // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
644 if ( 'name' == $orderby )
645 uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') );
647 uasort( $tags, create_function('$a, $b', 'return ($a->count > $b->count);') );
649 if ( 'DESC' == $order )
650 $tags = array_reverse( $tags, true );
655 $tags = array_slice($tags, 0, $number);
658 $real_counts = array(); // For the alt tag
659 foreach ( (array) $tags as $key => $tag ) {
660 $real_counts[ $key ] = $tag->count;
661 $counts[ $key ] = $topic_count_scale_callback($tag->count);
664 $min_count = min( $counts );
665 $spread = max( $counts ) - $min_count;
668 $font_spread = $largest - $smallest;
669 if ( $font_spread < 0 )
671 $font_step = $font_spread / $spread;
675 foreach ( $tags as $key => $tag ) {
676 $count = $counts[ $key ];
677 $real_count = $real_counts[ $key ];
678 $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
679 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
680 $tag_name = $tags[ $key ]->name;
681 $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: " .
682 ( $smallest + ( ( $count - $min_count ) * $font_step ) )
683 . "$unit;'>$tag_name</a>";
691 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
692 $return .= join( "</li>\n\t<li>", $a );
693 $return .= "</li>\n</ul>\n";
696 $return = join( $separator, $a );
701 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
707 * Callback for comparing tags based on name
712 function _wp_tag_cloud_name_sort_cb( $a, $b ) {
713 return strnatcasecmp( $a->name, $b->name );
717 * Callback for comparing tags based on count
722 function _wp_tag_cloud_count_sort_cb( $a, $b ) {
723 return ( $a->count > $b->count );
731 * Retrieve HTML list content for category list.
733 * @uses Walker_Category to create HTML list content.
735 * @see Walker_Category::walk() for parameters and return description.
737 function walk_category_tree() {
738 $args = func_get_args();
739 // the user's options are the third parameter
740 if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
741 $walker = new Walker_Category;
743 $walker = $args[2]['walker'];
745 return call_user_func_array(array( &$walker, 'walk' ), $args );
749 * Retrieve HTML dropdown (select) content for category list.
751 * @uses Walker_CategoryDropdown to create HTML dropdown content.
753 * @see Walker_CategoryDropdown::walk() for parameters and return description.
755 function walk_category_dropdown_tree() {
756 $args = func_get_args();
757 // the user's options are the third parameter
758 if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
759 $walker = new Walker_CategoryDropdown;
761 $walker = $args[2]['walker'];
763 return call_user_func_array(array( &$walker, 'walk' ), $args );
767 * Create HTML list of categories.
773 class Walker_Category extends Walker {
775 * @see Walker::$tree_type
779 var $tree_type = 'category';
782 * @see Walker::$db_fields
784 * @todo Decouple this
787 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
790 * @see Walker::start_lvl()
793 * @param string $output Passed by reference. Used to append additional content.
794 * @param int $depth Depth of category. Used for tab indentation.
795 * @param array $args Will only append content if style argument value is 'list'.
797 function start_lvl(&$output, $depth, $args) {
798 if ( 'list' != $args['style'] )
801 $indent = str_repeat("\t", $depth);
802 $output .= "$indent<ul class='children'>\n";
806 * @see Walker::end_lvl()
809 * @param string $output Passed by reference. Used to append additional content.
810 * @param int $depth Depth of category. Used for tab indentation.
811 * @param array $args Will only append content if style argument value is 'list'.
813 function end_lvl(&$output, $depth, $args) {
814 if ( 'list' != $args['style'] )
817 $indent = str_repeat("\t", $depth);
818 $output .= "$indent</ul>\n";
822 * @see Walker::start_el()
825 * @param string $output Passed by reference. Used to append additional content.
826 * @param object $category Category data object.
827 * @param int $depth Depth of category in reference to parents.
830 function start_el(&$output, $category, $depth, $args) {
833 $cat_name = esc_attr( $category->name );
834 $cat_name = apply_filters( 'list_cats', $cat_name, $category );
835 $link = '<a href="' . esc_attr( get_term_link($category) ) . '" ';
836 if ( $use_desc_for_title == 0 || empty($category->description) )
837 $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
839 $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
841 $link .= $cat_name . '</a>';
843 if ( !empty($feed_image) || !empty($feed) ) {
846 if ( empty($feed_image) )
849 $link .= '<a href="' . get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) . '"';
851 if ( empty($feed) ) {
852 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
854 $title = ' title="' . $feed . '"';
855 $alt = ' alt="' . $feed . '"';
862 if ( empty($feed_image) )
865 $link .= "<img src='$feed_image'$alt$title" . ' />';
869 if ( empty($feed_image) )
873 if ( !empty($show_count) )
874 $link .= ' (' . intval($category->count) . ')';
876 if ( !empty($show_date) )
877 $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
879 if ( 'list' == $args['style'] ) {
881 $class = 'cat-item cat-item-' . $category->term_id;
882 if ( !empty($current_category) ) {
883 $_current_category = get_term( $current_category, $category->taxonomy );
884 if ( $category->term_id == $current_category )
885 $class .= ' current-cat';
886 elseif ( $category->term_id == $_current_category->parent )
887 $class .= ' current-cat-parent';
889 $output .= ' class="' . $class . '"';
890 $output .= ">$link\n";
892 $output .= "\t$link<br />\n";
897 * @see Walker::end_el()
900 * @param string $output Passed by reference. Used to append additional content.
901 * @param object $page Not used.
902 * @param int $depth Depth of category. Not used.
903 * @param array $args Only uses 'list' for whether should append to output.
905 function end_el(&$output, $page, $depth, $args) {
906 if ( 'list' != $args['style'] )
909 $output .= "</li>\n";
915 * Create HTML dropdown list of Categories.
921 class Walker_CategoryDropdown extends Walker {
923 * @see Walker::$tree_type
927 var $tree_type = 'category';
930 * @see Walker::$db_fields
932 * @todo Decouple this
935 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
938 * @see Walker::start_el()
941 * @param string $output Passed by reference. Used to append additional content.
942 * @param object $category Category data object.
943 * @param int $depth Depth of category. Used for padding.
944 * @param array $args Uses 'selected', 'show_count', and 'show_last_update' keys, if they exist.
946 function start_el(&$output, $category, $depth, $args) {
947 $pad = str_repeat(' ', $depth * 3);
949 $cat_name = apply_filters('list_cats', $category->name, $category);
950 $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
951 if ( $category->term_id == $args['selected'] )
952 $output .= ' selected="selected"';
954 $output .= $pad.$cat_name;
955 if ( $args['show_count'] )
956 $output .= ' ('. $category->count .')';
957 if ( $args['show_last_update'] ) {
959 $output .= ' ' . gmdate($format, $category->last_update_timestamp);
961 $output .= "</option>\n";
970 * Retrieve the link to the tag.
973 * @see get_term_link()
975 * @param int|object $tag Tag ID or object.
976 * @return string Link on success, empty string if tag does not exist.
978 function get_tag_link( $tag ) {
979 if ( ! is_object( $tag ) )
982 $tag = get_term_link( $tag, 'post_tag' );
984 if ( is_wp_error( $tag ) )
991 * Retrieve the tags for a post.
994 * @uses apply_filters() Calls 'get_the_tags' filter on the list of post tags.
996 * @param int $id Post ID.
999 function get_the_tags( $id = 0 ) {
1000 return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
1004 * Retrieve the tags for a post formatted as a string.
1007 * @uses apply_filters() Calls 'the_tags' filter on string list of tags.
1009 * @param string $before Optional. Before tags.
1010 * @param string $sep Optional. Between tags.
1011 * @param string $after Optional. After tags.
1014 function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
1015 return apply_filters( 'the_tags', get_the_term_list( 0, 'post_tag', $before, $sep, $after ), $before, $sep, $after);
1019 * Retrieve the tags for a post.
1023 * @param string $before Optional. Before list.
1024 * @param string $sep Optional. Separate items using this.
1025 * @param string $after Optional. After list.
1028 function the_tags( $before = null, $sep = ', ', $after = '' ) {
1029 if ( null === $before )
1030 $before = __('Tags: ');
1031 echo get_the_tag_list($before, $sep, $after);
1035 * Retrieve tag description.
1039 * @param int $tag Optional. Tag ID. Will use global tag ID by default.
1040 * @return string Tag description, available.
1042 function tag_description( $tag = 0 ) {
1043 return term_description( $tag );
1047 * Retrieve term description.
1051 * @param int $term Optional. Term ID. Will use global term ID by default.
1052 * @return string Term description, available.
1054 function term_description( $term = 0, $taxonomy = 'post_tag' ) {
1055 if ( !$term && ( is_tax() || is_tag() || is_category() ) ) {
1056 $term = get_queried_object();
1057 $taxonomy = $term->taxonomy;
1058 $term = $term->term_id;
1060 $description = get_term_field( 'description', $term, $taxonomy );
1061 return is_wp_error( $description ) ? '' : $description;
1065 * Retrieve the terms of the taxonomy that are attached to the post.
1069 * @param int $id Post ID. Is not optional.
1070 * @param string $taxonomy Taxonomy name.
1071 * @return array|bool False on failure. Array of term objects on success.
1073 function get_the_terms( $id = 0, $taxonomy ) {
1082 $id = (int) $post->ID;
1085 $terms = get_object_term_cache( $id, $taxonomy );
1086 if ( false === $terms ) {
1087 $terms = wp_get_object_terms( $id, $taxonomy );
1088 wp_cache_add($id, $terms, $taxonomy . '_relationships');
1091 $terms = apply_filters( 'get_the_terms', $terms, $id, $taxonomy );
1093 if ( empty( $terms ) )
1100 * Retrieve a post's terms as a list with specified format.
1104 * @param int $id Post ID.
1105 * @param string $taxonomy Taxonomy name.
1106 * @param string $before Optional. Before list.
1107 * @param string $sep Optional. Separate items using this.
1108 * @param string $after Optional. After list.
1111 function get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '' ) {
1112 $terms = get_the_terms( $id, $taxonomy );
1114 if ( is_wp_error( $terms ) )
1117 if ( empty( $terms ) )
1120 foreach ( $terms as $term ) {
1121 $link = get_term_link( $term, $taxonomy );
1122 if ( is_wp_error( $link ) )
1124 $term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
1127 $term_links = apply_filters( "term_links-$taxonomy", $term_links );
1129 return $before . join( $sep, $term_links ) . $after;
1133 * Display the terms in a list.
1137 * @param int $id Post ID.
1138 * @param string $taxonomy Taxonomy name.
1139 * @param string $before Optional. Before list.
1140 * @param string $sep Optional. Separate items using this.
1141 * @param string $after Optional. After list.
1142 * @return null|bool False on WordPress error. Returns null when displaying.
1144 function the_terms( $id = 0, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
1145 $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
1147 if ( is_wp_error( $term_list ) )
1150 echo apply_filters('the_terms', $term_list, $taxonomy, $before, $sep, $after);
1155 * Check if the current post has any of given category.
1159 * @param string|int|array $tag Optional. The category name/term_id/slug or array of them to check for.
1160 * @param int|object $post Optional. Post to check instead of the current post.
1161 * @return bool True if the current post has any of the given categories (or any category, if no category specified).
1163 function has_category( $category = '', $post = null ) {
1164 return has_term( $category, 'category', $post );
1168 * Check if the current post has any of given tags.
1170 * The given tags are checked against the post's tags' term_ids, names and slugs.
1171 * Tags given as integers will only be checked against the post's tags' term_ids.
1172 * If no tags are given, determines if post has any tags.
1174 * 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)
1175 * Prior to v2.7, this function could only be used in the WordPress Loop.
1176 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
1180 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
1181 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
1182 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1184 function has_tag( $tag = '', $post = null ) {
1185 return has_term( $tag, 'post_tag', $post );
1189 * Check if the current post has any of given terms.
1191 * The given terms are checked against the post's terms' term_ids, names and slugs.
1192 * Terms given as integers will only be checked against the post's terms' term_ids.
1193 * If no terms are given, determines if post has any terms.
1197 * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
1198 * @param string $taxonomy Taxonomy name
1199 * @param int|object $post Optional. Post to check instead of the current post.
1200 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1202 function has_term( $term = '', $taxonomy = '', $post = null ) {
1203 $post = get_post($post);
1208 $r = is_object_in_term( $post->ID, $taxonomy, $term );
1209 if ( is_wp_error( $r ) )