3 function get_the_category($id = false) {
4 global $post, $category_cache;
10 if ( !isset($category_cache[$id]) )
11 update_post_category_cache($id);
13 $categories = $category_cache[$id];
15 if ( !empty($categories) )
18 $categories = array();
23 function get_category_link($category_id) {
25 $catlink = $wp_rewrite->get_category_permastruct();
27 if ( empty($catlink) ) {
28 $file = get_settings('home') . '/';
29 $catlink = $file . '?cat=' . $category_id;
31 $category = &get_category($category_id);
32 $category_nicename = $category->category_nicename;
34 if ( $parent = $category->category_parent )
35 $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename . '/';
37 $catlink = str_replace('%category%', $category_nicename, $catlink);
38 $catlink = get_settings('home') . trailingslashit($catlink);
40 return apply_filters('category_link', $catlink, $category_id);
43 function get_the_category_list($separator = '', $parents='') {
44 $categories = get_the_category();
45 if (empty($categories))
46 return apply_filters('the_category', __('Uncategorized'), $separator, $parents);
49 if ( '' == $separator ) {
50 $thelist .= '<ul class="post-categories">';
51 foreach ( $categories as $category ) {
52 $category->cat_name = $category->cat_name;
53 $thelist .= "\n\t<li>";
54 switch ( strtolower($parents) ) {
56 if ($category->category_parent)
57 $thelist .= get_category_parents($category->category_parent, TRUE);
58 $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>';
61 $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . ' rel="category tag">';
62 if ($category->category_parent)
63 $thelist .= get_category_parents($category->category_parent, FALSE);
64 $thelist .= $category->cat_name.'</a></li>';
68 $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>';
74 foreach ( $categories as $category ) {
75 $category->cat_name = $category->cat_name;
77 $thelist .= $separator . ' ';
78 switch ( strtolower($parents) ) {
80 if ( $category->category_parent )
81 $thelist .= get_category_parents($category->category_parent, TRUE);
82 $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>';
85 $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">';
86 if ( $category->category_parent )
87 $thelist .= get_category_parents($category->category_parent, FALSE);
88 $thelist .= "$category->cat_name</a>";
92 $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>';
97 return apply_filters('the_category', $thelist, $separator, $parents);
100 function the_category($separator = '', $parents='') {
101 echo get_the_category_list($separator, $parents);
104 function get_the_category_by_ID($cat_ID) {
105 $cat_ID = (int) $cat_ID;
106 $category = &get_category($cat_ID);
107 return $category->cat_name;
110 function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){
112 $parent = &get_category($id);
115 $name = $parent->category_nicename;
117 $name = $parent->cat_name;
119 if ( $parent->category_parent )
120 $chain .= get_category_parents($parent->category_parent, $link, $separator, $nicename);
123 $chain .= '<a href="' . get_category_link($parent->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator;
125 $chain .= $name.$separator;
129 function get_category_children($id, $before = '/', $after = '') {
133 $cat_ids = get_all_category_ids();
134 foreach ( $cat_ids as $cat_id ) {
138 $category = get_category($cat_id);
139 if ( $category->category_parent == $id ) {
140 $chain .= $before.$category->cat_ID.$after;
141 $chain .= get_category_children($category->cat_ID, $before, $after);
148 function the_category_ID($echo = true) {
149 // Grab the first cat in the list.
150 $categories = get_the_category();
151 $cat = $categories[0]->cat_ID;
160 function the_category_head($before='', $after='') {
161 global $currentcat, $previouscat;
162 // Grab the first cat in the list.
163 $categories = get_the_category();
164 $currentcat = $categories[0]->category_id;
165 if ( $currentcat != $previouscat ) {
167 echo get_the_category_by_ID($currentcat);
169 $previouscat = $currentcat;
173 function category_description($category = 0) {
177 $category = & get_category($category);
178 return apply_filters('category_description', $category->category_description, $category->cat_ID);
181 // out of the WordPress loop
182 function dropdown_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc',
183 $optiondates = 0, $optioncount = 0, $hide_empty = 1, $optionnone=FALSE,
184 $selected=0, $hide=0) {
186 if ( ($file == 'blah') || ($file == '') )
187 $file = get_settings('home') . '/';
190 $sort_column = 'cat_'.$sort_column;
193 SELECT cat_ID, cat_name, category_nicename,category_parent,
194 COUNT($wpdb->post2cat.post_id) AS cat_count,
195 DAYOFMONTH(MAX(post_date)) AS lastday, MONTH(MAX(post_date)) AS lastmonth
196 FROM $wpdb->categories LEFT JOIN $wpdb->post2cat ON (cat_ID = category_id)
197 LEFT JOIN $wpdb->posts ON (ID = post_id)
201 $query .= " AND cat_ID != $hide";
202 $query .= get_category_children($hide, " AND cat_ID != ");
204 $query .=" GROUP BY cat_ID";
205 if ( intval($hide_empty) == 1 )
206 $query .= " HAVING cat_count > 0";
207 $query .= " ORDER BY $sort_column $sort_order, post_date DESC";
209 $categories = $wpdb->get_results($query);
210 echo "<select name='cat' class='postform'>\n";
211 if ( intval($optionall) == 1 ) {
212 $all = apply_filters('list_cats', $all);
213 echo "\t<option value='0'>$all</option>\n";
215 if ( intval($optionnone) == 1 )
216 echo "\t<option value='-1'>".__('None')."</option>\n";
218 foreach ( $categories as $category ) {
219 $cat_name = apply_filters('list_cats', $category->cat_name, $category);
220 echo "\t<option value=\"".$category->cat_ID."\"";
221 if ( $category->cat_ID == $selected )
222 echo ' selected="selected"';
225 if ( intval($optioncount) == 1 )
226 echo ' ('.$category->cat_count.')';
227 if ( intval($optiondates) == 1 )
228 echo ' '.$category->lastday.'/'.$category->lastmonth;
235 // out of the WordPress loop
236 function wp_list_cats($args = '') {
237 parse_str($args, $r);
238 if ( !isset($r['optionall']))
240 if ( !isset($r['all']))
242 if ( !isset($r['sort_column']) )
243 $r['sort_column'] = 'ID';
244 if ( !isset($r['sort_order']) )
245 $r['sort_order'] = 'asc';
246 if ( !isset($r['file']) )
248 if ( !isset($r['list']) )
250 if ( !isset($r['optiondates']) )
251 $r['optiondates'] = 0;
252 if ( !isset($r['optioncount']) )
253 $r['optioncount'] = 0;
254 if ( !isset($r['hide_empty']) )
255 $r['hide_empty'] = 1;
256 if ( !isset($r['use_desc_for_title']) )
257 $r['use_desc_for_title'] = 1;
258 if ( !isset($r['children']) )
259 $r['children'] = true;
260 if ( !isset($r['child_of']) )
262 if ( !isset($r['categories']) )
263 $r['categories'] = 0;
264 if ( !isset($r['recurse']) )
266 if ( !isset($r['feed']) )
268 if ( !isset($r['feed_image']) )
269 $r['feed_image'] = '';
270 if ( !isset($r['exclude']) )
272 if ( !isset($r['hierarchical']) )
273 $r['hierarchical'] = true;
275 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']);
278 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) {
279 global $wpdb, $wp_query;
280 // Optiondates now works
282 $file = get_settings('home') . '/';
285 if ( !empty($exclude) ) {
286 $excats = preg_split('/[\s,]+/',$exclude);
287 if ( count($excats) ) {
288 foreach ( $excats as $excat ) {
289 $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
294 $exclusions = apply_filters('list_cats_exclusions', $exclusions );
296 if ( intval($categories) == 0 ) {
297 $sort_column = 'cat_'.$sort_column;
300 SELECT cat_ID, cat_name, category_nicename, category_description, category_parent, category_count
301 FROM $wpdb->categories
302 WHERE cat_ID > 0 $exclusions
303 ORDER BY $sort_column $sort_order";
305 $categories = $wpdb->get_results($query);
308 if ( $optiondates ) {
309 $cat_dates = $wpdb->get_results(" SELECT category_id,
310 UNIX_TIMESTAMP( MAX(post_date) ) AS ts
311 FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories
312 WHERE post_status = 'publish' AND post_id = ID $exclusions
313 GROUP BY category_id");
314 foreach ( $cat_dates as $cat_date ) {
315 $category_timestamp["$cat_date->category_id"] = $cat_date->ts;
322 foreach ( (array) $categories as $category ) {
323 if ( ( intval($hide_empty) == 0 || $category->category_count) && (!$hierarchical || $category->category_parent == $child_of) ) {
325 $link = '<a href="'.get_category_link($category->cat_ID).'" ';
326 if ( $use_desc_for_title == 0 || empty($category->category_description) )
327 $link .= 'title="'. sprintf(__("View all posts filed under %s"), attribute_escape($category->cat_name)) . '"';
329 $link .= 'title="' . attribute_escape(apply_filters('category_description',$category->category_description,$category)) . '"';
331 $link .= apply_filters('list_cats', $category->cat_name, $category).'</a>';
333 if ( (! empty($feed_image)) || (! empty($feed)) ) {
337 if ( empty($feed_image) )
340 $link .= '<a href="' . get_category_rss_link(0, $category->cat_ID, $category->category_nicename) . '"';
342 if ( !empty($feed) ) {
343 $title = ' title="' . $feed . '"';
344 $alt = ' alt="' . $feed . '"';
351 if ( !empty($feed_image) )
352 $link .= "<img src='$feed_image' $alt$title" . ' />';
358 if (empty($feed_image))
362 if ( intval($optioncount) == 1 )
363 $link .= ' ('.intval($category->category_count).')';
365 if ( $optiondates ) {
366 if ( $optiondates == 1 )
367 $optiondates = 'Y-m-d';
368 $link .= ' ' . gmdate($optiondates, $category_timestamp["$category->cat_ID"]);
373 if (($category->cat_ID == $wp_query->get_queried_object_id()) && is_category()) {
374 $thelist .= ' class="current-cat"';
376 $thelist .= ">$link\n";
378 $thelist .= "\t$link<br />\n";
381 if ($hierarchical && $children)
382 $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);
384 $thelist .= "</li>\n";
387 if ( !$num_found && !$child_of ) {
392 echo $before . __("No categories") . $after . "\n";
395 if ( $list && $child_of && $num_found && $recurse ) {
396 $pre = "\t\t<ul class='children'>";
397 $post = "\t\t</ul>\n";
401 $thelist = $pre . $thelist . $post;
404 echo apply_filters('list_cats', $thelist);
407 function in_category($category) { // Check if the current post is in the given category
408 global $category_cache, $post;
410 if ( isset( $category_cache[$post->ID][$category] ) )