Support » Requests and Feedback » display parent cat if child has posts

  • if you use wp_list_cats() with hide_empty=1 empty parent categories will not be shown and also their children won’t be.

    Below is a patch which changes this behaviour, you can see it in action on blog.tomk32.de where i have hide_empty=1


    diff -u template-functions-category*
    --- template-functions-category-tomk32.php 2005-08-31 09:48:10.000000000 +0200
    +++ template-functions-category.php 2005-08-14 20:06:02.000000000 +0200
    @@ -273,13 +273,12 @@
    WHERE cat_ID > 0 $exclusions
    ORDER BY $sort_column $sort_order";

    - $categories = $wpdb->get_results($query);
    + $categories = $wpdb->get_results($query);
    }
    -
    if (!count($category_posts)) {
    $now = current_time('mysql', 1);
    $cat_counts = $wpdb->get_results(" SELECT cat_ID,
    - COUNT($wpdb->post2cat.post_id) AS cat_count, category_parent
    + COUNT($wpdb->post2cat.post_id) AS cat_count
    FROM $wpdb->categories
    INNER JOIN $wpdb->post2cat ON (cat_ID = category_id)
    INNER JOIN $wpdb->posts ON (ID = post_id)
    @@ -287,15 +286,10 @@
    AND post_date_gmt < '$now' $exclusions
    GROUP BY category_id");
    if (! empty($cat_counts)) {
    - foreach ($categories as $cat) {
    - if (count_category_children($cat_counts, $cat->cat_ID) > 0) {
    - $category_posts[$cat->cat_ID] = 0;
    - }
    - }
    foreach ($cat_counts as $cat_count) {
    - if( 1 != intval($hide_empty) || $cat_count > 0) {
    - $category_posts["$cat_count->cat_ID"] = $cat_count->cat_count;
    - }
    + if (1 != intval($hide_empty) || $cat_count > 0) {
    + $category_posts["$cat_count->cat_ID"] = $cat_count->cat_count;
    + }
    }
    }
    }
    @@ -395,19 +389,6 @@
    echo apply_filters('list_cats', $thelist);
    }

    -
    -function count_category_children(&$categories, $cat_id) {
    - $result = 0;
    - foreach ($categories as $cat) {
    - if ($cat->category_parent == $cat_id) {
    - $result += count_category_children($categories, $cat->cat_ID);
    - $result += $cat->cat_count;
    - }
    - }
    - return $result;
    -}
    -
    -
    function in_category($category) { // Check if the current post is in the given category
    global $post, $category_cache;
    $cats = '';

Viewing 2 replies - 1 through 2 (of 2 total)
  • jwurster

    (@jwurster)

    Could this code be modified to correctly total the child posts and display for the parent total instead of zero?

    You can also use the function in the coffee2code recent post plugin. Nice work though. 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘display parent cat if child has posts’ is closed to new replies.