diff --git a/b2-include/b2template.functions.php b/b2-include/b2template.functions.php index 299f33c0a1..1ca021e278 100644 --- a/b2-include/b2template.functions.php +++ b/b2-include/b2template.functions.php @@ -860,27 +860,50 @@ function dropdown_cats($optionall = 1, $all = 'All') { } // out of the b2 loop -function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc', $file = 'blah', $list = true) { - global $tablecategories, $querycount, $wpdb; +function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc', + $file = 'blah', $list = true, $optiondates = 0, $optioncount = 0, $hide_empty = 1) { + global $tablecategories, $tableposts, $querycount, $wpdb; global $pagenow; global $querystring_start, $querystring_equal, $querystring_separator; - $file = ($file == 'blah') ? $pagenow : $file; + if (($file == 'blah') || ($file == '')) { + $file = $pagenow; + } $sort_column = 'cat_'.$sort_column; - $categories = $wpdb->get_results("SELECT * FROM $tablecategories WHERE cat_ID > 0 ORDER BY $sort_column $sort_order"); + + $query = " SELECT cat_ID, cat_name,"; + $query .= " COUNT($tableposts.ID) AS cat_count,"; + $query .= " DAYOFMONTH(MAX(post_date)) AS lastday, MONTH(MAX(post_date)) AS lastmonth"; + $query .= " FROM $tablecategories LEFT JOIN $tableposts ON cat_ID = post_category"; + $query .= " WHERE cat_ID > 0 "; + $query .= " GROUP BY post_category "; + if (intval($hide_empty) == 1) { + $query .= " HAVING cat_count > 0"; + } + $query .= " ORDER BY $sort_column $sort_order, post_date DESC"; + + $categories = $wpdb->get_results($query); ++$querycount; if (intval($optionall) == 1) { $all = apply_filters('list_cats', $all); - if ($list) echo "\n\t
  • '.$all."
  • "; - else echo "\t'.$all."
    \n"; + $link = "'.$all.""; + if ($list) echo "\n\t
  • $link
  • "; + else echo "\t$link
    \n"; } foreach ($categories as $category) { $cat_name = apply_filters('list_cats', $category->cat_name); + $link = "cat_ID.'">'; + $link .= stripslashes($cat_name).""; + if (intval($optioncount) == 1) { + $link .= "  (".$category->cat_count.")"; + } + if (intval($optiondates) == 1) { + $link .= "  ".$category->lastday."/".$category->lastmonth.""; + } + $link .= ""; if ($list) { - echo "\n\t
  • cat_ID.'">'; - echo stripslashes($cat_name)."
  • "; + echo "\n\t
  • $link
  • "; } else { - echo "\tcat_ID.'">'; - echo stripslashes($cat_name)."
    \n"; + echo "\t$link
    \n"; } } }