3 function get_category_children($id, $before = '/', $after = '', $visited=array()) {
8 // TODO: consult hierarchy
9 $cat_ids = get_all_category_ids();
10 foreach ( $cat_ids as $cat_id ) {
14 $category = get_category($cat_id);
15 if ( is_wp_error( $category ) )
17 if ( $category->parent == $id && !in_array($category->term_id, $visited) ) {
18 $visited[] = $category->term_id;
19 $chain .= $before.$category->term_id.$after;
20 $chain .= get_category_children($category->term_id, $before, $after);
26 function get_category_link($category_id) {
28 $catlink = $wp_rewrite->get_category_permastruct();
30 if ( empty($catlink) ) {
31 $file = get_option('home') . '/';
32 $catlink = $file . '?cat=' . $category_id;
34 $category = &get_category($category_id);
35 if ( is_wp_error( $category ) )
37 $category_nicename = $category->slug;
39 if ( $parent = $category->parent )
40 $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename;
42 $catlink = str_replace('%category%', $category_nicename, $catlink);
43 $catlink = get_option('home') . user_trailingslashit($catlink, 'category');
45 return apply_filters('category_link', $catlink, $category_id);
48 function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE, $visited = array()){
50 $parent = &get_category($id);
51 if ( is_wp_error( $parent ) )
55 $name = $parent->slug;
57 $name = $parent->cat_name;
59 if ( $parent->parent && ($parent->parent != $parent->term_id) && !in_array($parent->parent, $visited) ) {
60 $visited[] = $parent->parent;
61 $chain .= get_category_parents($parent->parent, $link, $separator, $nicename, $visited);
65 $chain .= '<a href="' . get_category_link($parent->term_id) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator;
67 $chain .= $name.$separator;
71 function get_the_category($id = false) {
76 $id = (int) $post->ID;
78 $categories = get_object_term_cache($id, 'category');
79 if ( false === $categories )
80 $categories = wp_get_object_terms($id, 'category');
82 if ( !empty($categories) )
83 usort($categories, '_usort_terms_by_name');
85 $categories = array();
87 foreach(array_keys($categories) as $key) {
88 _make_cat_compat($categories[$key]);
94 function _usort_terms_by_name($a, $b) {
95 return strcmp($a->name, $b->name);
98 function _usort_terms_by_ID($a, $b) {
99 if ( $a->term_id > $b->term_id )
101 elseif ( $a->term_id < $b->term_id )
107 function get_the_category_by_ID($cat_ID) {
108 $cat_ID = (int) $cat_ID;
109 $category = &get_category($cat_ID);
110 if ( is_wp_error( $category ) )
112 return $category->name;
115 function get_the_category_list($separator = '', $parents='', $post_id = false) {
117 $categories = get_the_category($post_id);
118 if (empty($categories))
119 return apply_filters('the_category', __('Uncategorized'), $separator, $parents);
121 $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
124 if ( '' == $separator ) {
125 $thelist .= '<ul class="post-categories">';
126 foreach ( $categories as $category ) {
127 $thelist .= "\n\t<li>";
128 switch ( strtolower($parents) ) {
130 if ($category->parent)
131 $thelist .= get_category_parents($category->parent, TRUE);
132 $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->name.'</a></li>';
135 $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>';
136 if ($category->parent)
137 $thelist .= get_category_parents($category->parent, FALSE);
138 $thelist .= $category->name.'</a></li>';
142 $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->cat_name.'</a></li>';
148 foreach ( $categories as $category ) {
150 $thelist .= $separator . ' ';
151 switch ( strtolower($parents) ) {
153 if ( $category->parent )
154 $thelist .= get_category_parents($category->parent, TRUE);
155 $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->cat_name.'</a>';
158 $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>';
159 if ( $category->parent )
160 $thelist .= get_category_parents($category->parent, FALSE);
161 $thelist .= "$category->cat_name</a>";
165 $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->name.'</a>';
170 return apply_filters('the_category', $thelist, $separator, $parents);
174 * in_category() - Checks whether the current post is within a particular category
176 * This function checks to see if the post is within the supplied category. The categoy
177 * can be specified by number or name and will be checked as a name first to allow for categories with numeric names.
178 * Note: Prior to v2.5 of WordPress category names where not supported.
182 * @param int|string $category
183 * @return bool true if the post is in the supplied category
185 function in_category( $category ) { // Check if the current post is in the given category
188 if ( empty($category) )
191 // If category is not an int, check to see if it's a name
192 if ( ! is_int($category) ) {
193 $cat_ID = get_cat_ID($category);
198 $categories = get_object_term_cache($post->ID, 'category');
199 if ( false === $categories )
200 $categories = wp_get_object_terms($post->ID, 'category');
201 if ( array_key_exists($category, $categories) )
207 function the_category($separator = '', $parents='', $post_id = false) {
208 echo get_the_category_list($separator, $parents, $post_id);
211 function category_description($category = 0) {
216 return get_term_field('description', $category, 'category');
219 function wp_dropdown_categories($args = '') {
221 'show_option_all' => '', 'show_option_none' => '',
222 'orderby' => 'ID', 'order' => 'ASC',
223 'show_last_update' => 0, 'show_count' => 0,
224 'hide_empty' => 1, 'child_of' => 0,
225 'exclude' => '', 'echo' => 1,
226 'selected' => 0, 'hierarchical' => 0,
227 'name' => 'cat', 'class' => 'postform',
228 'depth' => 0, 'tab_index' => 0
231 $defaults['selected'] = ( is_category() ) ? get_query_var('cat') : 0;
233 $r = wp_parse_args( $args, $defaults );
234 $r['include_last_update_time'] = $r['show_last_update'];
237 $tab_index_attribute = '';
238 if ( (int) $tab_index > 0 )
239 $tab_index_attribute = " tabindex=\"$tab_index\"";
241 $categories = get_categories($r);
244 if ( ! empty($categories) ) {
245 $output = "<select name='$name' id='$name' class='$class' $tab_index_attribute>\n";
247 if ( $show_option_all ) {
248 $show_option_all = apply_filters('list_cats', $show_option_all);
249 $output .= "\t<option value='0'>$show_option_all</option>\n";
252 if ( $show_option_none) {
253 $show_option_none = apply_filters('list_cats', $show_option_none);
254 $output .= "\t<option value='-1'>$show_option_none</option>\n";
258 $depth = $r['depth']; // Walk the full depth.
260 $depth = -1; // Flat.
262 $output .= walk_category_dropdown_tree($categories, $depth, $r);
263 $output .= "</select>\n";
266 $output = apply_filters('wp_dropdown_cats', $output);
274 function wp_list_categories($args = '') {
276 'show_option_all' => '', 'orderby' => 'name',
277 'order' => 'ASC', 'show_last_update' => 0,
278 'style' => 'list', 'show_count' => 0,
279 'hide_empty' => 1, 'use_desc_for_title' => 1,
280 'child_of' => 0, 'feed' => '', 'feed_type' => '',
281 'feed_image' => '', 'exclude' => '', 'current_category' => 0,
282 'hierarchical' => true, 'title_li' => __('Categories'),
283 'echo' => 1, 'depth' => 0
286 $r = wp_parse_args( $args, $defaults );
288 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
289 $r['pad_counts'] = true;
292 if ( isset( $r['show_date'] ) ) {
293 $r['include_last_update_time'] = $r['show_date'];
298 $categories = get_categories($r);
301 if ( $title_li && 'list' == $style )
302 $output = '<li class="categories">' . $r['title_li'] . '<ul>';
304 if ( empty($categories) ) {
305 if ( 'list' == $style )
306 $output .= '<li>' . __("No categories") . '</li>';
308 $output .= __("No categories");
312 if( !empty($show_option_all) )
313 if ('list' == $style )
314 $output .= '<li><a href="' . get_bloginfo('url') . '">' . $show_option_all . '</a></li>';
316 $output .= '<a href="' . get_bloginfo('url') . '">' . $show_option_all . '</a>';
318 if ( empty( $r['current_category'] ) && is_category() )
319 $r['current_category'] = $wp_query->get_queried_object_id();
322 $depth = $r['depth'];
324 $depth = -1; // Flat.
326 $output .= walk_category_tree($categories, $depth, $r);
329 if ( $title_li && 'list' == $style )
330 $output .= '</ul></li>';
332 $output = apply_filters('wp_list_categories', $output);
340 function wp_tag_cloud( $args = '' ) {
342 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
343 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
344 'exclude' => '', 'include' => ''
346 $args = wp_parse_args( $args, $defaults );
348 $tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
353 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
355 if ( is_wp_error( $return ) )
358 $return = apply_filters( 'wp_tag_cloud', $return, $args );
360 if ( 'array' == $args['format'] )
366 // $tags = prefetched tag array ( get_tags() )
367 // $args['format'] = 'flat' => whitespace separated, 'list' => UL, 'array' => array()
368 // $args['orderby'] = 'name', 'count'
369 function wp_generate_tag_cloud( $tags, $args = '' ) {
372 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
373 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
375 $args = wp_parse_args( $args, $defaults );
380 $counts = $tag_links = array();
381 foreach ( (array) $tags as $tag ) {
382 $counts[$tag->name] = $tag->count;
383 $tag_links[$tag->name] = get_tag_link( $tag->term_id );
384 if ( is_wp_error( $tag_links[$tag->name] ) )
385 return $tag_links[$tag->name];
386 $tag_ids[$tag->name] = $tag->term_id;
389 $min_count = min($counts);
390 $spread = max($counts) - $min_count;
393 $font_spread = $largest - $smallest;
394 if ( $font_spread <= 0 )
396 $font_step = $font_spread / $spread;
398 // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
399 if ( 'name' == $orderby )
400 uksort($counts, 'strnatcasecmp');
404 if ( 'DESC' == $order )
405 $counts = array_reverse( $counts, true );
406 elseif ( 'RAND' == $order ) {
407 $keys = array_rand( $counts, count($counts) );
408 foreach ( $keys as $key )
409 $temp[$key] = $counts[$key];
416 $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
418 foreach ( $counts as $tag => $count ) {
419 $tag_id = $tag_ids[$tag];
420 $tag_link = clean_url($tag_links[$tag]);
421 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . attribute_escape( sprintf( __ngettext('%d topic','%d topics',$count), $count ) ) . "'$rel style='font-size: " .
422 ( $smallest + ( ( $count - $min_count ) * $font_step ) )
423 . "$unit;'>$tag</a>";
431 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
432 $return .= join("</li>\n\t<li>", $a);
433 $return .= "</li>\n</ul>\n";
436 $return = join("\n", $a);
440 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
447 function walk_category_tree() {
448 $walker = new Walker_Category;
449 $args = func_get_args();
450 return call_user_func_array(array(&$walker, 'walk'), $args);
453 function walk_category_dropdown_tree() {
454 $walker = new Walker_CategoryDropdown;
455 $args = func_get_args();
456 return call_user_func_array(array(&$walker, 'walk'), $args);
463 function get_tag_link( $tag_id ) {
465 $taglink = $wp_rewrite->get_tag_permastruct();
467 $tag = &get_term($tag_id, 'post_tag');
468 if ( is_wp_error( $tag ) )
472 if ( empty($taglink) ) {
473 $file = get_option('home') . '/';
474 $taglink = $file . '?tag=' . $slug;
476 $taglink = str_replace('%tag%', $slug, $taglink);
477 $taglink = get_option('home') . user_trailingslashit($taglink, 'category');
479 return apply_filters('tag_link', $taglink, $tag_id);
482 function get_the_tags( $id = 0 ) {
483 return apply_filters( 'get_the_tags', get_the_terms($id, 'post_tag') );
486 function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
487 return apply_filters( 'the_tags', get_the_term_list(0, 'post_tag', $before, $sep, $after) );
490 function the_tags( $before = 'Tags: ', $sep = ', ', $after = '' ) {
491 return the_terms( 0, 'post_tag', $before, $sep, $after );
494 function get_the_terms( $id = 0, $taxonomy ) {
499 if ( ! $id && ! in_the_loop() )
500 return false; // in-the-loop function
503 $id = (int) $post->ID;
505 $terms = get_object_term_cache($id, $taxonomy);
506 if ( false === $terms )
507 $terms = wp_get_object_terms($id, $taxonomy);
509 if ( empty( $terms ) )
515 function get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '' ) {
516 $terms = get_the_terms($id, $taxonomy);
518 if ( is_wp_error($terms) )
521 if ( empty( $terms ) )
524 foreach ( $terms as $term ) {
525 $link = get_term_link($term, $taxonomy);
526 if ( is_wp_error( $link ) )
528 $term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
531 $term_links = apply_filters( "term_links-$taxonomy", $term_links );
533 return $before . join($sep, $term_links) . $after;
536 function the_terms( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
537 $return = get_the_term_list($id, $taxonomy, $before, $sep, $after);
538 if ( is_wp_error( $return ) )
545 * Check if the current post has the given tag
550 * @uses wp_get_object_terms() Gets the tags.
552 * @param string|int|array $tag Optional. The tag name/id/slug or array of them to check for
553 * @return bool True if the current post has the given tag, or any tag, if no tag specified
555 function has_tag($tag = '') {
557 $taxonomy = 'post_tag';
559 if ( !in_the_loop() ) return false; // in-the-loop function
561 $post_id = (int) $post->ID;
563 $terms = get_object_term_cache($post_id, $taxonomy);
565 $terms = wp_get_object_terms($post_id, $taxonomy);
566 if (empty($terms)) return false;
568 if (empty($tag)) return (!empty($terms));
572 foreach($terms as $term) {
573 if ( in_array( $term->term_id, $tag ) ) return true;
574 if ( in_array( $term->name, $tag ) ) return true;
575 if ( in_array( $term->slug, $tag ) ) return true;