| [ Index ] |
WordPress Source Cross Reference |
[Summary view] [Print] [Text view]
1 <?php 2 3 function get_category_children($id, $before = '/', $after = '') { 4 if ( 0 == $id ) 5 return ''; 6 7 $cat_ids = get_all_category_ids(); 8 foreach ( $cat_ids as $cat_id ) { 9 if ( $cat_id == $id) 10 continue; 11 12 $category = get_category($cat_id); 13 if ( $category->category_parent == $id ) { 14 $chain .= $before.$category->cat_ID.$after; 15 $chain .= get_category_children($category->cat_ID, $before, $after); 16 } 17 } 18 return $chain; 19 } 20 21 function get_category_link($category_id) { 22 global $wp_rewrite; 23 $catlink = $wp_rewrite->get_category_permastruct(); 24 25 if ( empty($catlink) ) { 26 $file = get_settings('home') . '/'; 27 $catlink = $file . '?cat=' . $category_id; 28 } else { 29 $category = &get_category($category_id); 30 $category_nicename = $category->category_nicename; 31 32 if ( $parent = $category->category_parent ) 33 $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename . '/'; 34 35 $catlink = str_replace('%category%', $category_nicename, $catlink); 36 $catlink = get_settings('home') . trailingslashit($catlink); 37 } 38 return apply_filters('category_link', $catlink, $category_id); 39 } 40 41 function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){ 42 $chain = ''; 43 $parent = &get_category($id); 44 45 if ( $nicename ) 46 $name = $parent->category_nicename; 47 else 48 $name = $parent->cat_name; 49 50 if ( $parent->category_parent ) 51 $chain .= get_category_parents($parent->category_parent, $link, $separator, $nicename); 52 53 if ( $link ) 54 $chain .= '<a href="' . get_category_link($parent->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator; 55 else 56 $chain .= $name.$separator; 57 return $chain; 58 } 59 60 function get_the_category($id = false) { 61 global $post, $category_cache; 62 63 if ( !$id ) 64 $id = $post->ID; 65 66 if ( !isset($category_cache[$id]) ) 67 update_post_category_cache($id); 68 69 $categories = $category_cache[$id]; 70 71 if ( !empty($categories) ) 72 sort($categories); 73 else 74 $categories = array(); 75 76 return $categories; 77 } 78 79 function get_the_category_by_ID($cat_ID) { 80 $cat_ID = (int) $cat_ID; 81 $category = &get_category($cat_ID); 82 return $category->cat_name; 83 } 84 85 function get_the_category_list($separator = '', $parents='') { 86 $categories = get_the_category(); 87 if (empty($categories)) 88 return apply_filters('the_category', __('Uncategorized'), $separator, $parents); 89 90 $thelist = ''; 91 if ( '' == $separator ) { 92 $thelist .= '<ul class="post-categories">'; 93 foreach ( $categories as $category ) { 94 $thelist .= "\n\t<li>"; 95 switch ( strtolower($parents) ) { 96 case 'multiple': 97 if ($category->category_parent) 98 $thelist .= get_category_parents($category->category_parent, TRUE); 99 $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a></li>'; 100 break; 101 case 'single': 102 $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . ' rel="category tag">'; 103 if ($category->category_parent) 104 $thelist .= get_category_parents($category->category_parent, FALSE); 105 $thelist .= $category->cat_name.'</a></li>'; 106 break; 107 case '': 108 default: 109 $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a></li>'; 110 } 111 } 112 $thelist .= '</ul>'; 113 } else { 114 $i = 0; 115 foreach ( $categories as $category ) { 116 if ( 0 < $i ) 117 $thelist .= $separator . ' '; 118 switch ( strtolower($parents) ) { 119 case 'multiple': 120 if ( $category->category_parent ) 121 $thelist .= get_category_parents($category->category_parent, TRUE); 122 $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a>'; 123 break; 124 case 'single': 125 $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'; 126 if ( $category->category_parent ) 127 $thelist .= get_category_parents($category->category_parent, FALSE); 128 $thelist .= "$category->cat_name</a>"; 129 break; 130 case '': 131 default: 132 $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a>'; 133 } 134 ++$i; 135 } 136 } 137 return apply_filters('the_category', $thelist, $separator, $parents); 138 } 139 140 function in_category($category) { // Check if the current post is in the given category 141 global $category_cache, $post; 142 143 if ( isset( $category_cache[$post->ID][$category] ) ) 144 return true; 145 else 146 return false; 147 } 148 149 function the_category($separator = '', $parents='') { 150 echo get_the_category_list($separator, $parents); 151 } 152 153 function category_description($category = 0) { 154 global $cat; 155 if ( !$category ) 156 $category = $cat; 157 $category = & get_category($category); 158 return apply_filters('category_description', $category->category_description, $category->cat_ID); 159 } 160 161 function wp_dropdown_categories($args = '') { 162 if ( is_array($args) ) 163 $r = &$args; 164 else 165 parse_str($args, $r); 166 167 $defaults = array('show_option_all' => '', 'show_option_none' => '', 'orderby' => 'ID', 168 'order' => 'ASC', 'show_last_update' => 0, 'show_count' => 0, 169 'hide_empty' => 1, 'child_of' => 0, 'exclude' => '', 'echo' => 1, 170 'selected' => 0, 'hierarchical' => 0, 'name' => 'cat', 171 'class' => 'postform'); 172 $r = array_merge($defaults, $r); 173 $r['include_last_update_time'] = $r['show_last_update']; 174 extract($r); 175 176 $categories = get_categories($r); 177 178 $output = ''; 179 if ( ! empty($categories) ) { 180 $output = "<select name='$name' class='$class'>\n"; 181 182 if ( $show_option_all ) { 183 $show_option_all = apply_filters('list_cats', $show_option_all); 184 $output .= "\t<option value='0'>$show_option_all</option>\n"; 185 } 186 187 if ( $show_option_none) { 188 $show_option_none = apply_filters('list_cats', $show_option_none); 189 $output .= "\t<option value='-1'>$show_option_none</option>\n"; 190 } 191 192 if ( $hierarchical ) 193 $depth = 0; // Walk the full depth. 194 else 195 $depth = -1; // Flat. 196 197 $output .= walk_category_dropdown_tree($categories, $depth, $r); 198 $output .= "</select>\n"; 199 } 200 201 $output = apply_filters('wp_dropdown_cats', $output); 202 203 if ( $echo ) 204 echo $output; 205 206 return $output; 207 } 208 209 function wp_list_categories($args = '') { 210 if ( is_array($args) ) 211 $r = &$args; 212 else 213 parse_str($args, $r); 214 215 $defaults = array('show_option_all' => '', 'orderby' => 'name', 216 'order' => 'ASC', 'show_last_update' => 0, 'style' => 'list', 217 'show_count' => 0, 'hide_empty' => 1, 'use_desc_for_title' => 1, 218 'child_of' => 0, 'feed' => '', 'feed_image' => '', 'exclude' => '', 219 'hierarchical' => true, 'title_li' => __('Categories')); 220 $r = array_merge($defaults, $r); 221 $r['include_last_update_time'] = $r['show_date']; 222 extract($r); 223 224 $categories = get_categories($r); 225 226 $output = ''; 227 if ( $title_li && 'list' == $style ) 228 $output = '<li class="categories">' . $r['title_li'] . '<ul>'; 229 230 if ( empty($categories) ) { 231 if ( $list) 232 $output .= '<li>' . __("No categories") . '</li>'; 233 else 234 $output .= __("No categories"); 235 } else { 236 global $wp_query; 237 $r['current_category'] = $wp_query->get_queried_object_id(); 238 if ( $hierarchical ) 239 $depth = 0; // Walk the full depth. 240 else 241 $depth = -1; // Flat. 242 243 $output .= walk_category_tree($categories, $depth, $r); 244 } 245 246 if ( $title_li && 'list' == $style ) 247 $output .= '</ul></li>'; 248 249 echo apply_filters('list_cats', $output); 250 } 251 252 // 253 // Helper functions 254 // 255 256 function walk_category_tree() { 257 $walker = new Walker_Category; 258 $args = func_get_args(); 259 return call_user_func_array(array(&$walker, 'walk'), $args); 260 } 261 262 function walk_category_dropdown_tree() { 263 $walker = new Walker_CategoryDropdown; 264 $args = func_get_args(); 265 return call_user_func_array(array(&$walker, 'walk'), $args); 266 } 267 268 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Jul 15 11:57:04 2006 | Courtesy of Taragana |