3 function get_the_category($id = false) {
4 global $post, $category_cache;
9 if ( !isset($category_cache[$id]) )
10 update_post_category_cache($id);
12 $categories = $category_cache[$id];
14 if ( !empty($categories) )
17 $categories = array();
22 function get_category_link($category_id) {
24 $catlink = $wp_rewrite->get_category_permastruct();
26 if ( empty($catlink) ) {
27 $file = get_settings('home') . '/';
28 $catlink = $file . '?cat=' . $category_id;
30 $category = &get_category($category_id);
31 $category_nicename = $category->category_nicename;
33 if ( $parent = $category->category_parent )
34 $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename . '/';
36 $catlink = str_replace('%category%', $category_nicename, $catlink);
37 $catlink = get_settings('home') . trailingslashit($catlink);
39 return apply_filters('category_link', $catlink, $category_id);
42 function get_the_category_list($separator = '', $parents='') {
43 $categories = get_the_category();
44 if (empty($categories))
45 return apply_filters('the_category', __('Uncategorized'), $separator, $parents);
48 if ( '' == $separator ) {
49 $thelist .= '<ul class="post-categories">';
50 foreach ( $categories as $category ) {
51 $category->cat_name = $category->cat_name;
52 $thelist .= "\n\t<li>";
53 switch ( strtolower($parents) ) {
55 if ($category->category_parent)
56 $thelist .= get_category_parents($category->category_parent, TRUE);
57 $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>';
60 $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . ' rel="category tag">';
61 if ($category->category_parent)
62 $thelist .= get_category_parents($category->category_parent, FALSE);
63 $thelist .= $category->cat_name.'</a></li>';
67 $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>';
73 foreach ( $categories as $category ) {
74 $category->cat_name = $category->cat_name;
76 $thelist .= $separator . ' ';
77 switch ( strtolower($parents) ) {
79 if ( $category->category_parent )
80 $thelist .= get_category_parents($category->category_parent, TRUE);
81 $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>';
84 $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">';
85 if ( $category->category_parent )
86 $thelist .= get_category_parents($category->category_parent, FALSE);
87 $thelist .= "$category->cat_name</a>";
91 $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>';
96 return apply_filters('the_category', $thelist, $separator, $parents);
99 function the_category($separator = '', $parents='') {
100 echo get_the_category_list($separator, $parents);
103 function get_the_category_by_ID($cat_ID) {
104 $cat_ID = (int) $cat_ID;
105 $category = &get_category($cat_ID);
106 return $category->cat_name;
109 function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){
111 $parent = &get_category($id);
114 $name = $parent->category_nicename;
116 $name = $parent->cat_name;
118 if ( $parent->category_parent )
119 $chain .= get_category_parents($parent->category_parent, $link, $separator, $nicename);
122 $chain .= '<a href="' . get_category_link($parent->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator;
124 $chain .= $name.$separator;
128 function get_category_children($id, $before = '/', $after = '') {
132 $cat_ids = get_all_category_ids();
133 foreach ( $cat_ids as $cat_id ) {
137 $category = get_category($cat_id);
138 if ( $category->category_parent == $id ) {
139 $chain .= $before.$category->cat_ID.$after;
140 $chain .= get_category_children($category->cat_ID, $before, $after);
147 function the_category_ID($echo = true) {
148 // Grab the first cat in the list.
149 $categories = get_the_category();
150 $cat = $categories[0]->cat_ID;
159 function the_category_head($before='', $after='') {
160 global $currentcat, $previouscat;
161 // Grab the first cat in the list.
162 $categories = get_the_category();
163 $currentcat = $categories[0]->category_id;
164 if ( $currentcat != $previouscat ) {
166 echo get_the_category_by_ID($currentcat);
168 $previouscat = $currentcat;
172 function category_description($category = 0) {
176 $category = & get_category($category);
177 return apply_filters('category_description', $category->category_description, $category->cat_ID);
180 // out of the WordPress loop
181 function dropdown_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc',
182 $optiondates = 0, $optioncount = 0, $hide_empty = 1, $optionnone=FALSE,
183 $selected=0, $hide=0) {
185 if ( ($file == 'blah') || ($file == '') )
186 $file = get_settings('home') . '/';
189 $sort_column = 'cat_'.$sort_column;
192 SELECT cat_ID, cat_name, category_nicename,category_parent,
193 COUNT($wpdb->post2cat.post_id) AS cat_count,
194 DAYOFMONTH(MAX(post_date)) AS lastday, MONTH(MAX(post_date)) AS lastmonth
195 FROM $wpdb->categories LEFT JOIN $wpdb->post2cat ON (cat_ID = category_id)
196 LEFT JOIN $wpdb->posts ON (ID = post_id)
200 $query .= " AND cat_ID != $hide";
201 $query .= get_category_children($hide, " AND cat_ID != ");
203 $query .=" GROUP BY cat_ID";
204 if ( intval($hide_empty) == 1 )
205 $query .= " HAVING cat_count > 0";
206 $query .= " ORDER BY $sort_column $sort_order, post_date DESC";
208 $categories = $wpdb->get_results($query);
209 echo "<select name='cat' class='postform'>\n";
210 if ( intval($optionall) == 1 ) {
211 $all = apply_filters('list_cats', $all);
212 echo "\t<option value='0'>$all</option>\n";
214 if ( intval($optionnone) == 1 )
215 echo "\t<option value='-1'>".__('None')."</option>\n";
217 foreach ( $categories as $category ) {
218 $cat_name = apply_filters('list_cats', $category->cat_name, $category);
219 echo "\t<option value=\"".$category->cat_ID."\"";
220 if ( $category->cat_ID == $selected )
221 echo ' selected="selected"';
224 if ( intval($optioncount) == 1 )
225 echo ' ('.$category->cat_count.')';
226 if ( intval($optiondates) == 1 )
227 echo ' '.$category->lastday.'/'.$category->lastmonth;
234 // out of the WordPress loop
235 function wp_list_cats($args = '') {
236 parse_str($args, $r);
237 if ( !isset($r['optionall']))
239 if ( !isset($r['all']))
241 if ( !isset($r['sort_column']) )
242 $r['sort_column'] = 'ID';
243 if ( !isset($r['sort_order']) )
244 $r['sort_order'] = 'asc';
245 if ( !isset($r['file']) )
247 if ( !isset($r['list']) )
249 if ( !isset($r['optiondates']) )
250 $r['optiondates'] = 0;
251 if ( !isset($r['optioncount']) )
252 $r['optioncount'] = 0;
253 if ( !isset($r['hide_empty']) )
254 $r['hide_empty'] = 1;
255 if ( !isset($r['use_desc_for_title']) )
256 $r['use_desc_for_title'] = 1;
257 if ( !isset($r['children']) )
258 $r['children'] = true;
259 if ( !isset($r['child_of']) )
261 if ( !isset($r['categories']) )
262 $r['categories'] = 0;
263 if ( !isset($r['recurse']) )
265 if ( !isset($r['feed']) )
267 if ( !isset($r['feed_image']) )
268 $r['feed_image'] = '';
269 if ( !isset($r['exclude']) )
271 if ( !isset($r['hierarchical']) )
272 $r['hierarchical'] = true;
274 return list_cats($r['optionall'], $r['all'], $r['sort_column'], $r['sort_order'], $r['file'], $r['list'], $r['optiondates'], $r['optioncount'], $r['hide_empty'], $r['use_desc_for_title'], $r['children'], $r['child_of'], $r['categories'], $r['recurse'], $r['feed'], $r['feed_image'], $r['exclude'], $r['hierarchical']);
277 function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc', $file = '', $list = true, $optiondates = 0, $optioncount = 0, $hide_empty = 1, $use_desc_for_title = 1, $children=FALSE, $child_of=0, $categories=0, $recurse=0, $feed = '', $feed_image = '', $exclude = '', $hierarchical=FALSE) {
278 global $wpdb, $wp_query;
279 // Optiondates now works
281 $file = get_settings('home') . '/';
284 if ( !empty($exclude) ) {
285 $excats = preg_split('/[\s,]+/',$exclude);
286 if ( count($excats) ) {
287 foreach ( $excats as $excat ) {
288 $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
293 $exclusions = apply_filters('list_cats_exclusions', $exclusions );
295 if ( intval($categories) == 0 ) {
296 $sort_column = 'cat_'.$sort_column;
299 SELECT cat_ID, cat_name, category_nicename, category_description, category_parent, category_count
300 FROM $wpdb->categories
301 WHERE cat_ID > 0 $exclusions
302 ORDER BY $sort_column $sort_order";
304 $categories = $wpdb->get_results($query);
307 if ( $optiondates ) {
308 $cat_dates = $wpdb->get_results(" SELECT category_id,
309 UNIX_TIMESTAMP( MAX(post_date) ) AS ts
310 FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories
311 WHERE post_status = 'publish' AND post_id = ID $exclusions
312 GROUP BY category_id");
313 foreach ( $cat_dates as $cat_date ) {
314 $category_timestamp["$cat_date->category_id"] = $cat_date->ts;
321 foreach ( $categories as $category ) {
322 if ( ( intval($hide_empty) == 0 || $category->category_count) && (!$hierarchical || $category->category_parent == $child_of) ) {
324 $link = '<a href="'.get_category_link($category->cat_ID).'" ';
325 if ( $use_desc_for_title == 0 || empty($category->category_description) )
326 $link .= 'title="'. sprintf(__("View all posts filed under %s"), wp_specialchars($category->cat_name)) . '"';
328 $link .= 'title="' . wp_specialchars(apply_filters('category_description',$category->category_description,$category)) . '"';
330 $link .= apply_filters('list_cats', $category->cat_name, $category).'</a>';
332 if ( (! empty($feed_image)) || (! empty($feed)) ) {
336 if ( empty($feed_image) )
339 $link .= '<a href="' . get_category_rss_link(0, $category->cat_ID, $category->category_nicename) . '"';
341 if ( !empty($feed) ) {
342 $title = ' title="' . $feed . '"';
343 $alt = ' alt="' . $feed . '"';
350 if ( !empty($feed_image) )
351 $link .= "<img src='$feed_image' $alt$title" . ' />';
357 if (empty($feed_image))
361 if ( intval($optioncount) == 1 )
362 $link .= ' ('.intval($category->category_count).')';
364 if ( $optiondates ) {
365 if ( $optiondates == 1 )
366 $optiondates = 'Y-m-d';
367 $link .= ' ' . gmdate($optiondates, $category_timestamp["$category->cat_ID"]);
372 if (($category->cat_ID == $wp_query->get_queried_object_id()) && is_category()) {
373 $thelist .= ' class="current-cat"';
375 $thelist .= ">$link\n";
377 $thelist .= "\t$link<br />\n";
380 if ($hierarchical && $children)
381 $thelist .= list_cats($optionall, $all, $sort_column, $sort_order, $file, $list, $optiondates, $optioncount, $hide_empty, $use_desc_for_title, $hierarchical, $category->cat_ID, $categories, 1, $feed, $feed_image, $exclude, $hierarchical);
383 $thelist .= "</li>\n";
386 if ( !$num_found && !$child_of ) {
391 echo $before . __("No categories") . $after . "\n";
394 if ( $list && $child_of && $num_found && $recurse ) {
395 $pre = "\t\t<ul class='children'>";
396 $post = "\t\t</ul>\n";
400 $thelist = $pre . $thelist . $post;
403 echo apply_filters('list_cats', $thelist);
406 function in_category($category) { // Check if the current post is in the given category
407 global $category_cache, $post;
409 if ( isset( $category_cache[$post->ID][$category] ) )