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) {
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='', $post_id = false) {
114 $categories = get_the_category($post_id);
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);
171 * in_category() - Checks whether the current post is within a particular category
173 * This function checks to see if the post is within the supplied category. The categoy
174 * can be specified by number or name and will be checked as a name first to allow for categories with numeric names.
175 * Note: Prior to v2.5 of WordPress category names where not supported.
179 * @param int|string $category
180 * @return bool true if the post is in the supplied category
182 function in_category( $category ) { // Check if the current post is in the given category
185 if ( empty($category) )
188 $cat_ID = get_cat_ID($category);
192 $categories = get_object_term_cache($post->ID, 'category');
193 if ( false === $categories )
194 $categories = wp_get_object_terms($post->ID, 'category');
195 if ( array_key_exists($category, $categories) )
201 function the_category($separator = '', $parents='', $post_id = false) {
202 echo get_the_category_list($separator, $parents, $post_id);
205 function category_description($category = 0) {
210 return get_term_field('description', $category, 'category');
213 function wp_dropdown_categories($args = '') {
215 'show_option_all' => '', 'show_option_none' => '',
216 'orderby' => 'ID', 'order' => 'ASC',
217 'show_last_update' => 0, 'show_count' => 0,
218 'hide_empty' => 1, 'child_of' => 0,
219 'exclude' => '', 'echo' => 1,
220 'selected' => 0, 'hierarchical' => 0,
221 'name' => 'cat', 'class' => 'postform',
222 'depth' => 0, 'tab_index' => 0
225 $defaults['selected'] = ( is_category() ) ? get_query_var('cat') : 0;
227 $r = wp_parse_args( $args, $defaults );
228 $r['include_last_update_time'] = $r['show_last_update'];
231 $tab_index_attribute = '';
232 if ( (int) $tab_index > 0 )
233 $tab_index_attribute = " tabindex=\"$tab_index\"";
235 $categories = get_categories($r);
238 if ( ! empty($categories) ) {
239 $output = "<select name='$name' id='$name' class='$class' $tab_index_attribute>\n";
241 if ( $show_option_all ) {
242 $show_option_all = apply_filters('list_cats', $show_option_all);
243 $output .= "\t<option value='0'>$show_option_all</option>\n";
246 if ( $show_option_none) {
247 $show_option_none = apply_filters('list_cats', $show_option_none);
248 $output .= "\t<option value='-1'>$show_option_none</option>\n";
252 $depth = $r['depth']; // Walk the full depth.
254 $depth = -1; // Flat.
256 $output .= walk_category_dropdown_tree($categories, $depth, $r);
257 $output .= "</select>\n";
260 $output = apply_filters('wp_dropdown_cats', $output);
268 function wp_list_categories($args = '') {
270 'show_option_all' => '', 'orderby' => 'name',
271 'order' => 'ASC', 'show_last_update' => 0,
272 'style' => 'list', 'show_count' => 0,
273 'hide_empty' => 1, 'use_desc_for_title' => 1,
274 'child_of' => 0, 'feed' => '', 'feed_type' => '',
275 'feed_image' => '', 'exclude' => '',
276 'hierarchical' => true, 'title_li' => __('Categories'),
277 'echo' => 1, 'depth' => 0
280 $r = wp_parse_args( $args, $defaults );
282 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
283 $r['pad_counts'] = true;
286 if ( isset( $r['show_date'] ) ) {
287 $r['include_last_update_time'] = $r['show_date'];
292 $categories = get_categories($r);
295 if ( $title_li && 'list' == $style )
296 $output = '<li class="categories">' . $r['title_li'] . '<ul>';
298 if ( empty($categories) ) {
299 if ( 'list' == $style )
300 $output .= '<li>' . __("No categories") . '</li>';
302 $output .= __("No categories");
306 if( !empty($show_option_all) )
307 if ('list' == $style )
308 $output .= '<li><a href="' . get_bloginfo('url') . '">' . $show_option_all . '</a></li>';
310 $output .= '<a href="' . get_bloginfo('url') . '">' . $show_option_all . '</a>';
313 $r['current_category'] = $wp_query->get_queried_object_id();
316 $depth = $r['depth'];
318 $depth = -1; // Flat.
320 $output .= walk_category_tree($categories, $depth, $r);
323 if ( $title_li && 'list' == $style )
324 $output .= '</ul></li>';
326 $output = apply_filters('wp_list_categories', $output);
334 function wp_tag_cloud( $args = '' ) {
336 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
337 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
338 'exclude' => '', 'include' => ''
340 $args = wp_parse_args( $args, $defaults );
342 $tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
347 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
349 if ( is_wp_error( $return ) )
352 $return = apply_filters( 'wp_tag_cloud', $return, $args );
354 if ( 'array' == $args['format'] )
360 // $tags = prefetched tag array ( get_tags() )
361 // $args['format'] = 'flat' => whitespace separated, 'list' => UL, 'array' => array()
362 // $args['orderby'] = 'name', 'count'
363 function wp_generate_tag_cloud( $tags, $args = '' ) {
366 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
367 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
369 $args = wp_parse_args( $args, $defaults );
374 $counts = $tag_links = array();
375 foreach ( (array) $tags as $tag ) {
376 $counts[$tag->name] = $tag->count;
377 $tag_links[$tag->name] = get_tag_link( $tag->term_id );
378 if ( is_wp_error( $tag_links[$tag->name] ) )
379 return $tag_links[$tag->name];
380 $tag_ids[$tag->name] = $tag->term_id;
383 $min_count = min($counts);
384 $spread = max($counts) - $min_count;
387 $font_spread = $largest - $smallest;
388 if ( $font_spread <= 0 )
390 $font_step = $font_spread / $spread;
392 // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
393 if ( 'name' == $orderby )
394 uksort($counts, 'strnatcasecmp');
398 if ( 'DESC' == $order )
399 $counts = array_reverse( $counts, true );
400 elseif ( 'RAND' == $order ) {
401 $keys = array_rand( $counts, count($counts) );
402 foreach ( $keys as $key )
403 $temp[$key] = $counts[$key];
410 $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
412 foreach ( $counts as $tag => $count ) {
413 $tag_id = $tag_ids[$tag];
414 $tag_link = clean_url($tag_links[$tag]);
415 $tag = str_replace(' ', ' ', wp_specialchars( $tag ));
416 $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: " .
417 ( $smallest + ( ( $count - $min_count ) * $font_step ) )
418 . "$unit;'>$tag</a>";
426 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
427 $return .= join("</li>\n\t<li>", $a);
428 $return .= "</li>\n</ul>\n";
431 $return = join("\n", $a);
435 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
442 function walk_category_tree() {
443 $walker = new Walker_Category;
444 $args = func_get_args();
445 return call_user_func_array(array(&$walker, 'walk'), $args);
448 function walk_category_dropdown_tree() {
449 $walker = new Walker_CategoryDropdown;
450 $args = func_get_args();
451 return call_user_func_array(array(&$walker, 'walk'), $args);
458 function get_tag_link( $tag_id ) {
460 $taglink = $wp_rewrite->get_tag_permastruct();
462 $tag = &get_term($tag_id, 'post_tag');
463 if ( is_wp_error( $tag ) )
467 if ( empty($taglink) ) {
468 $file = get_option('home') . '/';
469 $taglink = $file . '?tag=' . $slug;
471 $taglink = str_replace('%tag%', $slug, $taglink);
472 $taglink = get_option('home') . user_trailingslashit($taglink, 'category');
474 return apply_filters('tag_link', $taglink, $tag_id);
477 function get_the_tags( $id = 0 ) {
478 return apply_filters( 'get_the_tags', get_the_terms($id, 'post_tag') );
481 function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
482 return apply_filters( 'the_tags', get_the_term_list(0, 'post_tag', $before, $sep, $after) );
485 function the_tags( $before = 'Tags: ', $sep = ', ', $after = '' ) {
486 return the_terms( 0, 'post_tag', $before, $sep, $after );
489 function get_the_terms( $id = 0, $taxonomy ) {
494 if ( ! $id && ! in_the_loop() )
495 return false; // in-the-loop function
498 $id = (int) $post->ID;
500 $terms = get_object_term_cache($id, $taxonomy);
501 if ( false === $terms )
502 $terms = wp_get_object_terms($id, $taxonomy);
504 if ( empty( $terms ) )
510 function get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '' ) {
511 $terms = get_the_terms($id, $taxonomy);
513 if ( is_wp_error($terms) )
516 if ( empty( $terms ) )
519 foreach ( $terms as $term ) {
520 $link = get_term_link($term, $taxonomy);
521 if ( is_wp_error( $link ) )
523 $term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
526 $term_links = apply_filters( "term_links-$taxonomy", $term_links );
528 return $before . join($sep, $term_links) . $after;
531 function the_terms( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
532 $return = get_the_term_list($id, $taxonomy, $before, $sep, $after);
533 if ( is_wp_error( $return ) )