Make WordPress Core


Ignore:
Timestamp:
03/02/2006 04:51:24 AM (20 years ago)
Author:
ryan
Message:

Add wp_dropdown_categories(). Deprecate dropdown_categories(). Add wp_list_categories() and make wp_list_cats() an alias.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/template-functions-category.php

    r3589 r3591  
    150150}
    151151
    152 // out of the WordPress loop
    153 function dropdown_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc',
    154         $optiondates = 0, $optioncount = 0, $hide_empty = 1, $optionnone=FALSE,
    155         $selected=0, $hide=0) {
    156     global $wpdb;
    157     if ( ($file == 'blah') || ($file == '') )
    158         $file = get_settings('home') . '/';
    159     if ( !$selected )
    160         $selected=$cat;
    161     $sort_column = 'cat_'.$sort_column;
    162 
    163     $query = "
    164         SELECT cat_ID, cat_name, category_nicename,category_parent,
    165         COUNT($wpdb->post2cat.post_id) AS cat_count,
    166         DAYOFMONTH(MAX(post_date)) AS lastday, MONTH(MAX(post_date)) AS lastmonth
    167         FROM $wpdb->categories LEFT JOIN $wpdb->post2cat ON (cat_ID = category_id)
    168         LEFT JOIN $wpdb->posts ON (ID = post_id)
    169         WHERE cat_ID > 0
    170         ";
    171     if ( $hide ) {
    172         $query .= " AND cat_ID != $hide";
    173         $query .= get_category_children($hide, " AND cat_ID != ");
    174     }
    175     $query .=" GROUP BY cat_ID";
    176     if ( intval($hide_empty) == 1 )
    177         $query .= " HAVING cat_count > 0";
    178     $query .= " ORDER BY $sort_column $sort_order, post_date DESC";
    179 
    180     $categories = $wpdb->get_results($query);
    181     echo "<select name='cat' class='postform'>\n";
    182     if ( intval($optionall) == 1 ) {
    183         $all = apply_filters('list_cats', $all);
    184         echo "\t<option value='0'>$all</option>\n";
    185     }
    186     if ( intval($optionnone) == 1 )
    187         echo "\t<option value='-1'>".__('None')."</option>\n";
    188     if ( $categories ) {
    189         foreach ( $categories as $category ) {
    190             $cat_name = apply_filters('list_cats', $category->cat_name, $category);
    191             echo "\t<option value=\"".$category->cat_ID."\"";
    192             if ( $category->cat_ID == $selected )
    193                 echo ' selected="selected"';
    194             echo '>';
    195             echo $cat_name;
    196             if ( intval($optioncount) == 1 )
    197                 echo '&nbsp;&nbsp;('.$category->cat_count.')';
    198             if ( intval($optiondates) == 1 )
    199                 echo '&nbsp;&nbsp;'.$category->lastday.'/'.$category->lastmonth;
    200             echo "</option>\n";
    201         }
    202     }
    203     echo "</select>\n";
    204 }
    205 
    206 // out of the WordPress loop
     152function wp_dropdown_categories($args = '') {
     153    parse_str($args, $r);
     154    if ( !isset($r['show_option_all']))
     155        $r['show_option_all'] = '';
     156    if ( !isset($r['show_option_none']))
     157        $r['show_option_none'] = '';
     158    if ( !isset($r['orderby']) )
     159        $r['orderby'] = 'ID';
     160    if ( !isset($r['order']) )
     161        $r['order'] = 'ASC';
     162    if ( !isset($r['show_last_update']) )
     163        $r['show_last_update'] = 0;
     164    if ( !isset($r['show_counts']) )
     165        $r['show_counts'] = 0;
     166    if ( !isset($r['hide_empty']) )
     167        $r['hide_empty'] = 1;
     168    if ( !isset($r['child_of']) )
     169        $r['child_of'] = 0;
     170    if ( !isset($r['exclude']) )
     171        $r['exclude'] = '';
     172    if ( !isset($r['echo']) )
     173        $r['echo'] = 1;
     174    if ( !isset($r['selected']) )
     175        $r['selected'] = 0;
     176    if ( !isset($r['hierarchical']) )
     177        $r['hierarchical'] = 0;
     178    if ( !isset($r['name']) )
     179        $r['name'] = 'cat';
     180    if ( !isset($r['class']) )
     181        $r['class'] = 'postform';
     182
     183    $r['include_last_update_time'] = $r['show_last_update'];
     184
     185    extract($r);
     186
     187    $query = add_query_arg($r, '');
     188    $categories = get_categories($query);
     189
     190    $output = '';
     191    if ( ! empty($categories) ) {
     192        $output = "<select name='$name' class='$class'>\n";
     193
     194        if ( $show_option_all ) {
     195            $show_option_all = apply_filters('list_cats', $show_option_all);
     196            $output .= "\t<option value='0'>$show_option_all</option>\n";
     197        }
     198
     199        if ( $show_option_none) {
     200            $show_option_none = apply_filters('list_cats', $show_option_none);     
     201            $output .= "\t<option value='-1'>$show_option_none</option>\n";
     202        }
     203
     204        if ( $hierarchical )
     205            $depth = 0;  // Walk the full depth.
     206        else
     207            $depth = -1; // Flat.
     208
     209        $output .= walk_category_tree($categories, $depth, '_category_dropdown_element', '', '', '', $selected, $r);
     210        $output .= "</select>\n";
     211    }
     212
     213    $output = apply_filters('wp_dropdown_cats', $output);
     214
     215    if ( $echo )
     216        echo $output;
     217
     218    return $output;
     219}
     220
     221function _category_dropdown_element($output, $category, $depth, $selected, $args) {
     222    $pad = str_repeat('&nbsp;', $depth * 3);
     223
     224    $cat_name = apply_filters('list_cats', $category->cat_name, $category);
     225    $output .= "\t<option value=\"".$category->cat_ID."\"";
     226    if ( $category->cat_ID == $selected )
     227        $output .= ' selected="selected"';
     228    $output .= '>';
     229    $output .= $cat_name;
     230    if ( $args['show_counts'] )
     231        $output .= '&nbsp;&nbsp;('. $category->category_count .')';
     232    if ( $args['show_last_update'] ) {
     233        $format = 'Y-m-d';
     234        $output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
     235    }
     236    $output .= "</option>\n";
     237
     238    return $output;
     239}
     240
    207241function wp_list_cats($args = '') {
     242    return wp_list_categories($args);   
     243}
     244
     245function wp_list_categories($args = '') {
    208246    parse_str($args, $r);
    209247    if ( !isset($r['optionall']))
     
    239277    if ( !isset($r['title_li']) )
    240278        $r['title_li'] = '';
    241 
    242     $q['orderby'] = $r['sort_column'];
    243     $q['order'] = $r['sort_order'];
    244     $q['include_last_update_time'] = $r['optiondates'];
     279    if ( !isset($r['orderby']) )
     280        $r['orderby'] = $r['sort_column'];
     281    if ( !isset($r['order']) )
     282        $r['order'] = $r['sort_order'];     
     283    $r['include_last_update_time'] = $r['optiondates'];
    245284   
    246285    extract($r);
    247286
    248     $args = add_query_arg($q, $args);
    249     $categories = get_categories($args);
    250 
     287    $query = add_query_arg($r, '');
     288    $categories = get_categories($query);
     289   
    251290    $output = '';
    252291    if ( $title_li && $list )
     
    418457            foreach ( $excategories as $excat ) {
    419458                $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
     459                // TODO: Exclude children of excluded cats?
    420460            }
    421461        }
     
    436476        return array();
    437477
     478    // TODO: Integrate this into the main query.
    438479    if ( $include_last_update_time ) {
    439480        $stamps = $wpdb->get_results("SELECT category_id, UNIX_TIMESTAMP( MAX(post_date) ) AS ts FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories
Note: See TracChangeset for help on using the changeset viewer.