Ticket #2738: pad-counts.diff
| File pad-counts.diff, 4.7 KB (added by andy, 6 years ago) |
|---|
-
wp-includes/category.php
21 21 22 22 $defaults = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC', 23 23 'hide_empty' => true, 'include_last_update_time' => false, 'hierarchical' => 1, 'exclude' => '', 'include' => '', 24 'number' => '' );24 'number' => '', 'pad_counts' => false); 25 25 $r = array_merge($defaults, $r); 26 26 if ( 'count' == $r['orderby'] ) 27 27 $r['orderby'] = 'category_count'; … … 109 109 $categories = & _get_cat_children($child_of, $categories); 110 110 111 111 // 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 ) { 113 117 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); 116 120 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]); 118 126 } 119 if ( !$progeny && $hide_empty )120 unset($categories[$k]);121 else122 $categories[$k]->{'link' == $type ? 'link_count' : 'category_count'} = $progeny;123 127 } 124 128 } 125 129 reset ( $categories ); … … 130 134 return apply_filters('get_categories', $categories, $r); 131 135 } 132 136 137 // Recalculates link or post counts by including items from child categories 138 // Assumes all relevant children are already in the $categories argument 139 function pad_category_counts($type, &$categories) { 140 global $wpdb; 141 142 // Set up some useful arrays 143 foreach ( $categories as $key => $cat ) { 144 $cats[$cat->cat_ID] = & $categories[$key]; 145 $cat_IDs[] = $cat->cat_ID; 146 } 147 148 // Get the relevant post2cat or link2cat records and stick them in a lookup table 149 if ( $type == 'post' ) { 150 $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'"); 151 foreach ( $results as $row ) 152 ++$cat_items[$row->category_id][$row->post_id]; 153 } else { 154 $results = $wpdb->get_results($q="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'");echo $q; 155 foreach ( $results as $row ) 156 ++$cat_items[$row->category_id][$row->link_id]; 157 } 158 159 // Touch every ancestor's lookup row for each post in each category 160 foreach ( $cat_IDs as $cat_ID ) { 161 $child = $cat_ID; 162 while ( $parent = $cats[$child]->category_parent ) { 163 if ( !empty($cat_items[$cat_ID]) ) 164 foreach ( $cat_items[$cat_ID] as $item_id => $touches ) 165 ++$cat_items[$parent][$item_id]; 166 $child = $parent; 167 } 168 } 169 170 // Transfer the touched cells 171 foreach ( $cat_items as $id => $items ) 172 if ( isset($cats[$id]) ) 173 $cats[$id]->{'link' == $type ? 'link_count' : 'category_count'} = count($items); 174 } 175 133 176 // Retrieves category data given a category ID or category object. 134 177 // Handles category caching. 135 178 function &get_category(&$category, $output = OBJECT) { -
wp-includes/category-template.php
221 221 'child_of' => 0, 'feed' => '', 'feed_image' => '', 'exclude' => '', 222 222 'hierarchical' => true, 'title_li' => __('Categories')); 223 223 $r = array_merge($defaults, $r); 224 if ( !isset($r['pad_counts']) && $r['show_count'] && $r['hierarchical'] ) 225 $r['pad_counts'] = true; 224 226 if ( isset($r['show_date']) ) 225 227 $r['include_last_update_time'] = $r['show_date']; 226 228 extract($r); -
wp-content/themes/default/sidebar.php
46 46 </ul> 47 47 </li> 48 48 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>'); ?> 50 50 51 51 <?php /* If this is the frontpage */ if ( is_home() || is_page() ) { ?> 52 52 <?php wp_list_bookmarks(); ?>
