3 function get_category_children($id, $before = '/', $after = '') {
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 ) {
18 $chain .= $before.$category->term_id.$after;
19 $chain .= get_category_children($category->term_id, $before, $after);
25 function get_category_link($category_id) {
27 $catlink = $wp_rewrite->get_category_permastruct();
29 if ( empty($catlink) ) {
30 $file = get_option('home') . '/';
31 $catlink = $file . '?cat=' . $category_id;
33 $category = &get_category($category_id);
34 if ( is_wp_error( $category ) )
36 $category_nicename = $category->slug;
38 if ( $parent = $category->parent )
39 $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename;
41 $catlink = str_replace('%category%', $category_nicename, $catlink);
42 $catlink = get_option('home') . user_trailingslashit($catlink, 'category');
44 return apply_filters('category_link', $catlink, $category_id);
47 function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){
49 $parent = &get_category($id);
50 if ( is_wp_error( $parent ) )
54 $name = $parent->slug;
56 $name = $parent->cat_name;
58 if ( $parent->parent && ($parent->parent != $parent->term_id) )
59 $chain .= get_category_parents($parent->parent, $link, $separator, $nicename);
62 $chain .= '<a href="' . get_category_link($parent->term_id) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator;
64 $chain .= $name.$separator;
68 function get_the_category($id = false) {
69 global $post, $term_cache, $blog_id;
73 $id = (int) $post->ID;
75 $categories = get_object_term_cache($id, 'category');
76 if ( false === $categories )
77 $categories = wp_get_object_terms($id, 'category');
79 if ( !empty($categories) )
80 usort($categories, '_usort_terms_by_name');
82 $categories = array();
84 foreach(array_keys($categories) as $key) {
85 _make_cat_compat($categories[$key]);
91 function _usort_terms_by_name($a, $b) {
92 return strcmp($a->name, $b->name);
95 function _usort_terms_by_ID($a, $b) {
96 if ( $a->term_id > $b->term_id )
98 elseif ( $a->term_id < $b->term_id )
104 function get_the_category_by_ID($cat_ID) {
105 $cat_ID = (int) $cat_ID;
106 $category = &get_category($cat_ID);
107 if ( is_wp_error( $category ) )
109 return $category->name;
112 function get_the_category_list($separator = '', $parents='') {
114 $categories = get_the_category();
115 if (empty($categories))
116 return apply_filters('the_category', __('Uncategorized'), $separator, $parents);
118 $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
121 if ( '' == $separator ) {
122 $thelist .= '<ul class="post-categories">';
123 foreach ( $categories as $category ) {
124 $thelist .= "\n\t<li>";
125 switch ( strtolower($parents) ) {
127 if ($category->parent)
128 $thelist .= get_category_parents($category->parent, TRUE);
129 $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->name.'</a></li>';
132 $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>';
133 if ($category->parent)
134 $thelist .= get_category_parents($category->parent, FALSE);
135 $thelist .= $category->name.'</a></li>';
139 $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->cat_name.'</a></li>';
145 foreach ( $categories as $category ) {
147 $thelist .= $separator . ' ';
148 switch ( strtolower($parents) ) {
150 if ( $category->parent )
151 $thelist .= get_category_parents($category->parent, TRUE);
152 $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->cat_name.'</a>';
155 $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>';
156 if ( $category->parent )
157 $thelist .= get_category_parents($category->parent, FALSE);
158 $thelist .= "$category->cat_name</a>";
162 $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->name.'</a>';
167 return apply_filters('the_category', $thelist, $separator, $parents);
170 function in_category( $category ) { // Check if the current post is in the given category
171 global $post, $blog_id;
173 $categories = get_object_term_cache($post->ID, 'category');
174 if ( false === $categories )
175 $categories = wp_get_object_terms($post->ID, 'category');
176 if(array_key_exists($category, $categories))
182 function the_category($separator = '', $parents='') {
183 echo get_the_category_list($separator, $parents);
186 function category_description($category = 0) {
191 return get_term_field('description', $category, 'category');
194 function wp_dropdown_categories($args = '') {
196 'show_option_all' => '', 'show_option_none' => '',
197 'orderby' => 'ID', 'order' => 'ASC',
198 'show_last_update' => 0, 'show_count' => 0,
199 'hide_empty' => 1, 'child_of' => 0,
200 'exclude' => '', 'echo' => 1,
201 'selected' => 0, 'hierarchical' => 0,
202 'name' => 'cat', 'class' => 'postform'
205 $defaults['selected'] = ( is_category() ) ? get_query_var('cat') : 0;
207 $r = wp_parse_args( $args, $defaults );
208 $r['include_last_update_time'] = $r['show_last_update'];
211 $categories = get_categories($r);
214 if ( ! empty($categories) ) {
215 $output = "<select name='$name' id='$name' class='$class'>\n";
217 if ( $show_option_all ) {
218 $show_option_all = apply_filters('list_cats', $show_option_all);
219 $output .= "\t<option value='0'>$show_option_all</option>\n";
222 if ( $show_option_none) {
223 $show_option_none = apply_filters('list_cats', $show_option_none);
224 $output .= "\t<option value='-1'>$show_option_none</option>\n";
228 $depth = 0; // Walk the full depth.
230 $depth = -1; // Flat.
232 $output .= walk_category_dropdown_tree($categories, $depth, $r);
233 $output .= "</select>\n";
236 $output = apply_filters('wp_dropdown_cats', $output);
244 function wp_list_categories($args = '') {
246 'show_option_all' => '', 'orderby' => 'name',
247 'order' => 'ASC', 'show_last_update' => 0,
248 'style' => 'list', 'show_count' => 0,
249 'hide_empty' => 1, 'use_desc_for_title' => 1,
250 'child_of' => 0, 'feed' => '',
251 'feed_image' => '', 'exclude' => '',
252 'hierarchical' => true, 'title_li' => __('Categories'),
256 $r = wp_parse_args( $args, $defaults );
258 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
259 $r['pad_counts'] = true;
262 if ( isset( $r['show_date'] ) ) {
263 $r['include_last_update_time'] = $r['show_date'];
268 $categories = get_categories($r);
271 if ( $title_li && 'list' == $style )
272 $output = '<li class="categories">' . $r['title_li'] . '<ul>';
274 if ( empty($categories) ) {
275 if ( 'list' == $style )
276 $output .= '<li>' . __("No categories") . '</li>';
278 $output .= __("No categories");
282 if( !empty($show_option_all) )
283 if ('list' == $style )
284 $output .= '<li><a href="' . get_bloginfo('url') . '">' . $show_option_all . '</a></li>';
286 $output .= '<a href="' . get_bloginfo('url') . '">' . $show_option_all . '</a>';
289 $r['current_category'] = $wp_query->get_queried_object_id();
292 $depth = 0; // Walk the full depth.
294 $depth = -1; // Flat.
296 $output .= walk_category_tree($categories, $depth, $r);
299 if ( $title_li && 'list' == $style )
300 $output .= '</ul></li>';
302 $output = apply_filters('wp_list_categories', $output);
310 function wp_tag_cloud( $args = '' ) {
312 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
313 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
314 'exclude' => '', 'include' => ''
316 $args = wp_parse_args( $args, $defaults );
318 $tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
323 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
324 if ( is_wp_error( $return ) )
327 echo apply_filters( 'wp_tag_cloud', $return, $args );
330 // $tags = prefetched tag array ( get_tags() )
331 // $args['format'] = 'flat' => whitespace separated, 'list' => UL, 'array' => array()
332 // $args['orderby'] = 'name', 'count'
333 function wp_generate_tag_cloud( $tags, $args = '' ) {
336 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
337 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
339 $args = wp_parse_args( $args, $defaults );
344 $counts = $tag_links = array();
345 foreach ( (array) $tags as $tag ) {
346 $counts[$tag->name] = $tag->count;
347 $tag_links[$tag->name] = get_tag_link( $tag->term_id );
348 if ( is_wp_error( $tag_links[$tag->name] ) )
349 return $tag_links[$tag->name];
350 $tag_ids[$tag->name] = $tag->term_id;
353 $min_count = min($counts);
354 $spread = max($counts) - $min_count;
357 $font_spread = $largest - $smallest;
358 if ( $font_spread <= 0 )
360 $font_step = $font_spread / $spread;
362 // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
363 if ( 'name' == $orderby )
364 uksort($counts, 'strnatcasecmp');
368 if ( 'DESC' == $order )
369 $counts = array_reverse( $counts, true );
373 $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
375 foreach ( $counts as $tag => $count ) {
376 $tag_id = $tag_ids[$tag];
377 $tag_link = clean_url($tag_links[$tag]);
378 $tag = str_replace(' ', ' ', wp_specialchars( $tag ));
379 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . attribute_escape( sprintf( __('%d topics'), $count ) ) . "'$rel style='font-size: " .
380 ( $smallest + ( ( $count - $min_count ) * $font_step ) )
381 . "$unit;'>$tag</a>";
389 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
390 $return .= join("</li>\n\t<li>", $a);
391 $return .= "</li>\n</ul>\n";
394 $return = join("\n", $a);
398 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
405 function walk_category_tree() {
406 $walker = new Walker_Category;
407 $args = func_get_args();
408 return call_user_func_array(array(&$walker, 'walk'), $args);
411 function walk_category_dropdown_tree() {
412 $walker = new Walker_CategoryDropdown;
413 $args = func_get_args();
414 return call_user_func_array(array(&$walker, 'walk'), $args);
421 function get_tag_link( $tag_id ) {
423 $taglink = $wp_rewrite->get_tag_permastruct();
425 $tag = &get_term($tag_id, 'post_tag');
426 if ( is_wp_error( $tag ) )
430 if ( empty($taglink) ) {
431 $file = get_option('home') . '/';
432 $taglink = $file . '?tag=' . $slug;
434 $taglink = str_replace('%tag%', $slug, $taglink);
435 $taglink = get_option('home') . user_trailingslashit($taglink, 'category');
437 return apply_filters('tag_link', $taglink, $tag_id);
440 function get_the_tags( $id = 0 ) {
445 if ( ! $id && ! in_the_loop() )
446 return false; // in-the-loop function
449 $id = (int) $post->ID;
451 $tags = get_object_term_cache($id, 'post_tag');
452 if ( false === $tags )
453 $tags = wp_get_object_terms($id, 'post_tag');
455 $tags = apply_filters( 'get_the_tags', $tags );
456 if ( empty( $tags ) )
461 function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
462 $tags = get_the_tags();
464 if ( empty( $tags ) )
468 foreach ( $tags as $tag ) {
469 $link = get_tag_link($tag->term_id);
470 if ( is_wp_error( $link ) )
472 $tag_links[] = '<a href="' . $link . '" rel="tag">' . $tag->name . '</a>';
475 $tag_links = join( $sep, $tag_links );
476 $tag_links = apply_filters( 'the_tags', $tag_links );
477 $tag_list .= $tag_links;
484 function the_tags( $before = 'Tags: ', $sep = ', ', $after = '' ) {
485 $return = get_the_tag_list($before, $sep, $after);
486 if ( is_wp_error( $return ) )