Make WordPress Core

Changeset 4707


Ignore:
Timestamp:
01/09/2007 08:45:05 AM (20 years ago)
Author:
ryan
Message:

Hierarchical category count fixes from andy. fixes #2738

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-content/themes/default/sidebar.php

    r4585 r4707  
    4747                        </li>
    4848
    49                         <?php wp_list_categories('optioncount=1&title_li=<h2>Categories</h2>'); ?>
     49                        <?php wp_list_categories('show_count=1&title_li=<h2>Categories</h2>'); ?>
    5050
    5151                        <?php /* If this is the frontpage */ if ( is_home() || is_page() ) { ?>
  • trunk/wp-includes/category-template.php

    r4583 r4707  
    222222                'hierarchical' => true, 'title_li' => __('Categories'));
    223223        $r = array_merge($defaults, $r);
     224        if ( !isset($r['pad_counts']) && $r['show_count'] && $r['hierarchical'] )
     225                $r['pad_counts'] = true;
    224226        if ( isset($r['show_date']) )
    225227                $r['include_last_update_time'] = $r['show_date'];
  • trunk/wp-includes/category.php

    r4634 r4707  
    2222        $defaults = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC',
    2323                'hide_empty' => true, 'include_last_update_time' => false, 'hierarchical' => 1, 'exclude' => '', 'include' => '',
    24                 'number' => '');
     24                'number' => '', 'pad_counts' => false);
    2525        $r = array_merge($defaults, $r);
    2626        if ( 'count' == $r['orderby'] )
    … …  
    110110
    111111        // Update category counts to include children.
    112         if ( $hierarchical ) {
     112        if ( $pad_counts )
     113                _pad_category_counts($type, $categories);
     114
     115        // Make sure we show empty categories that have children.
     116        if ( $hierarchical && $hide_empty ) {
    113117                foreach ( $categories as $k => $category ) {
    114                         $progeny = 'link' == $type ? $category->link_count : $category->category_count;
    115                         if ( $children = _get_cat_children($category->cat_ID, $categories) ) {
     118                        if ( ! $category->{'link' == $type ? 'link_count' : 'category_count'} ) {
     119                                $children = _get_cat_children($category->cat_ID, $categories);
    116120                                foreach ( $children as $child )
    117                                         $progeny += 'link' == $type ? $child->link_count : $child->category_count;
     121                                        if ( $child->{'link' == $type ? 'link_count' : 'category_count'} )
     122                                                continue 2;
     123
     124                                // It really is empty
     125                                unset($categories[$k]);
    118126                        }
    119                         if ( !$progeny && $hide_empty )
    120                                 unset($categories[$k]);
    121                         else
    122                                 $categories[$k]->{'link' == $type ? 'link_count' : 'category_count'} = $progeny;
    123127                }
    124128        }
    … …  
    257261}
    258262
     263// Recalculates link or post counts by including items from child categories
     264// Assumes all relevant children are already in the $categories argument
     265function _pad_category_counts($type, &$categories) {
     266        global $wpdb;
     267
     268        // Set up some useful arrays
     269        foreach ( $categories as $key => $cat ) {
     270                $cats[$cat->cat_ID] = & $categories[$key];
     271                $cat_IDs[] = $cat->cat_ID;
     272        }
     273
     274        // Get the relevant post2cat or link2cat records and stick them in a lookup table
     275        if ( $type == 'post' ) {
     276                $results = $wpdb->get_results("SELECT post_id, category_id FROM $wpdb->post2cat LEFT JOIN $wpdb->posts ON post_id = ID WHERE category_id IN (".join(',', $cat_IDs).") AND post_type = 'post' AND post_status = 'publish'");
     277                foreach ( $results as $row )
     278                        ++$cat_items[$row->category_id][$row->post_id];
     279        } else {
     280                $results = $wpdb->get_results("SELECT $wpdb->link2cat.link_id, category_id FROM $wpdb->link2cat LEFT JOIN $wpdb->links USING (link_id) WHERE category_id IN (".join(',', $cat_IDs).") AND link_visible = 'Y'");
     281                foreach ( $results as $row )
     282                        ++$cat_items[$row->category_id][$row->link_id];
     283        }
     284
     285        // Touch every ancestor's lookup row for each post in each category
     286        foreach ( $cat_IDs as $cat_ID ) {
     287                $child = $cat_ID;
     288                while ( $parent = $cats[$child]->category_parent ) {
     289                        if ( !empty($cat_items[$cat_ID]) )
     290                                foreach ( $cat_items[$cat_ID] as $item_id => $touches )
     291                                        ++$cat_items[$parent][$item_id];
     292                        $child = $parent;
     293                }
     294        }
     295
     296        // Transfer the touched cells
     297        foreach ( $cat_items as $id => $items )
     298                if ( isset($cats[$id]) )
     299                        $cats[$id]->{'link' == $type ? 'link_count' : 'category_count'} = count($items);
     300}
     301
    259302?>
Note: See TracChangeset for help on using the changeset viewer.