Add wp_dropdown_categories(). Deprecate dropdown_categories(). Add wp_list_categories() and make wp_list_cats() an alias.
git-svn-id: https://develop.svn.wordpress.org/trunk@3591 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
25dc212067
commit
621da95d0d
|
@ -411,4 +411,22 @@ function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_orde
|
||||||
return wp_list_cats($query);
|
return wp_list_cats($query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function dropdown_cats($optionall = 1, $all = 'All', $orderby = 'ID', $order = 'asc',
|
||||||
|
$show_last_update = 0, $show_counts = 0, $hide_empty = 1, $optionnone = FALSE,
|
||||||
|
$selected = 0, $exclude = 0) {
|
||||||
|
|
||||||
|
$show_option_all = '';
|
||||||
|
if ( $optionall )
|
||||||
|
$show_option_all = $all;
|
||||||
|
|
||||||
|
$show_option_none = '';
|
||||||
|
if ( $optionnone )
|
||||||
|
$show_option_none = __('None');
|
||||||
|
|
||||||
|
$vars = compact('show_option_all', 'show_option_none', 'orderby', 'order',
|
||||||
|
'show_last_update', 'show_counts', 'hide_empty', 'selected', 'exclude');
|
||||||
|
$query = add_query_arg($vars, '');
|
||||||
|
return wp_dropdown_categories($query);
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -149,62 +149,100 @@ function category_description($category = 0) {
|
||||||
return apply_filters('category_description', $category->category_description, $category->cat_ID);
|
return apply_filters('category_description', $category->category_description, $category->cat_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
// out of the WordPress loop
|
function wp_dropdown_categories($args = '') {
|
||||||
function dropdown_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc',
|
parse_str($args, $r);
|
||||||
$optiondates = 0, $optioncount = 0, $hide_empty = 1, $optionnone=FALSE,
|
if ( !isset($r['show_option_all']))
|
||||||
$selected=0, $hide=0) {
|
$r['show_option_all'] = '';
|
||||||
global $wpdb;
|
if ( !isset($r['show_option_none']))
|
||||||
if ( ($file == 'blah') || ($file == '') )
|
$r['show_option_none'] = '';
|
||||||
$file = get_settings('home') . '/';
|
if ( !isset($r['orderby']) )
|
||||||
if ( !$selected )
|
$r['orderby'] = 'ID';
|
||||||
$selected=$cat;
|
if ( !isset($r['order']) )
|
||||||
$sort_column = 'cat_'.$sort_column;
|
$r['order'] = 'ASC';
|
||||||
|
if ( !isset($r['show_last_update']) )
|
||||||
|
$r['show_last_update'] = 0;
|
||||||
|
if ( !isset($r['show_counts']) )
|
||||||
|
$r['show_counts'] = 0;
|
||||||
|
if ( !isset($r['hide_empty']) )
|
||||||
|
$r['hide_empty'] = 1;
|
||||||
|
if ( !isset($r['child_of']) )
|
||||||
|
$r['child_of'] = 0;
|
||||||
|
if ( !isset($r['exclude']) )
|
||||||
|
$r['exclude'] = '';
|
||||||
|
if ( !isset($r['echo']) )
|
||||||
|
$r['echo'] = 1;
|
||||||
|
if ( !isset($r['selected']) )
|
||||||
|
$r['selected'] = 0;
|
||||||
|
if ( !isset($r['hierarchical']) )
|
||||||
|
$r['hierarchical'] = 0;
|
||||||
|
if ( !isset($r['name']) )
|
||||||
|
$r['name'] = 'cat';
|
||||||
|
if ( !isset($r['class']) )
|
||||||
|
$r['class'] = 'postform';
|
||||||
|
|
||||||
$query = "
|
$r['include_last_update_time'] = $r['show_last_update'];
|
||||||
SELECT cat_ID, cat_name, category_nicename,category_parent,
|
|
||||||
COUNT($wpdb->post2cat.post_id) AS cat_count,
|
|
||||||
DAYOFMONTH(MAX(post_date)) AS lastday, MONTH(MAX(post_date)) AS lastmonth
|
|
||||||
FROM $wpdb->categories LEFT JOIN $wpdb->post2cat ON (cat_ID = category_id)
|
|
||||||
LEFT JOIN $wpdb->posts ON (ID = post_id)
|
|
||||||
WHERE cat_ID > 0
|
|
||||||
";
|
|
||||||
if ( $hide ) {
|
|
||||||
$query .= " AND cat_ID != $hide";
|
|
||||||
$query .= get_category_children($hide, " AND cat_ID != ");
|
|
||||||
}
|
|
||||||
$query .=" GROUP BY cat_ID";
|
|
||||||
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);
|
extract($r);
|
||||||
echo "<select name='cat' class='postform'>\n";
|
|
||||||
if ( intval($optionall) == 1 ) {
|
$query = add_query_arg($r, '');
|
||||||
$all = apply_filters('list_cats', $all);
|
$categories = get_categories($query);
|
||||||
echo "\t<option value='0'>$all</option>\n";
|
|
||||||
}
|
$output = '';
|
||||||
if ( intval($optionnone) == 1 )
|
if ( ! empty($categories) ) {
|
||||||
echo "\t<option value='-1'>".__('None')."</option>\n";
|
$output = "<select name='$name' class='$class'>\n";
|
||||||
if ( $categories ) {
|
|
||||||
foreach ( $categories as $category ) {
|
if ( $show_option_all ) {
|
||||||
$cat_name = apply_filters('list_cats', $category->cat_name, $category);
|
$show_option_all = apply_filters('list_cats', $show_option_all);
|
||||||
echo "\t<option value=\"".$category->cat_ID."\"";
|
$output .= "\t<option value='0'>$show_option_all</option>\n";
|
||||||
if ( $category->cat_ID == $selected )
|
|
||||||
echo ' selected="selected"';
|
|
||||||
echo '>';
|
|
||||||
echo $cat_name;
|
|
||||||
if ( intval($optioncount) == 1 )
|
|
||||||
echo ' ('.$category->cat_count.')';
|
|
||||||
if ( intval($optiondates) == 1 )
|
|
||||||
echo ' '.$category->lastday.'/'.$category->lastmonth;
|
|
||||||
echo "</option>\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( $show_option_none) {
|
||||||
|
$show_option_none = apply_filters('list_cats', $show_option_none);
|
||||||
|
$output .= "\t<option value='-1'>$show_option_none</option>\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( $hierarchical )
|
||||||
|
$depth = 0; // Walk the full depth.
|
||||||
|
else
|
||||||
|
$depth = -1; // Flat.
|
||||||
|
|
||||||
|
$output .= walk_category_tree($categories, $depth, '_category_dropdown_element', '', '', '', $selected, $r);
|
||||||
|
$output .= "</select>\n";
|
||||||
}
|
}
|
||||||
echo "</select>\n";
|
|
||||||
|
$output = apply_filters('wp_dropdown_cats', $output);
|
||||||
|
|
||||||
|
if ( $echo )
|
||||||
|
echo $output;
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
function _category_dropdown_element($output, $category, $depth, $selected, $args) {
|
||||||
|
$pad = str_repeat(' ', $depth * 3);
|
||||||
|
|
||||||
|
$cat_name = apply_filters('list_cats', $category->cat_name, $category);
|
||||||
|
$output .= "\t<option value=\"".$category->cat_ID."\"";
|
||||||
|
if ( $category->cat_ID == $selected )
|
||||||
|
$output .= ' selected="selected"';
|
||||||
|
$output .= '>';
|
||||||
|
$output .= $cat_name;
|
||||||
|
if ( $args['show_counts'] )
|
||||||
|
$output .= ' ('. $category->category_count .')';
|
||||||
|
if ( $args['show_last_update'] ) {
|
||||||
|
$format = 'Y-m-d';
|
||||||
|
$output .= ' ' . gmdate($format, $category->last_update_timestamp);
|
||||||
|
}
|
||||||
|
$output .= "</option>\n";
|
||||||
|
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
// out of the WordPress loop
|
|
||||||
function wp_list_cats($args = '') {
|
function wp_list_cats($args = '') {
|
||||||
|
return wp_list_categories($args);
|
||||||
|
}
|
||||||
|
|
||||||
|
function wp_list_categories($args = '') {
|
||||||
parse_str($args, $r);
|
parse_str($args, $r);
|
||||||
if ( !isset($r['optionall']))
|
if ( !isset($r['optionall']))
|
||||||
$r['optionall'] = 0;
|
$r['optionall'] = 0;
|
||||||
|
@ -238,16 +276,17 @@ function wp_list_cats($args = '') {
|
||||||
$r['hierarchical'] = false;
|
$r['hierarchical'] = false;
|
||||||
if ( !isset($r['title_li']) )
|
if ( !isset($r['title_li']) )
|
||||||
$r['title_li'] = '';
|
$r['title_li'] = '';
|
||||||
|
if ( !isset($r['orderby']) )
|
||||||
$q['orderby'] = $r['sort_column'];
|
$r['orderby'] = $r['sort_column'];
|
||||||
$q['order'] = $r['sort_order'];
|
if ( !isset($r['order']) )
|
||||||
$q['include_last_update_time'] = $r['optiondates'];
|
$r['order'] = $r['sort_order'];
|
||||||
|
$r['include_last_update_time'] = $r['optiondates'];
|
||||||
|
|
||||||
extract($r);
|
extract($r);
|
||||||
|
|
||||||
$args = add_query_arg($q, $args);
|
$query = add_query_arg($r, '');
|
||||||
$categories = get_categories($args);
|
$categories = get_categories($query);
|
||||||
|
|
||||||
$output = '';
|
$output = '';
|
||||||
if ( $title_li && $list )
|
if ( $title_li && $list )
|
||||||
$output = '<li class="categories">' . $r['title_li'] . '<ul>';
|
$output = '<li class="categories">' . $r['title_li'] . '<ul>';
|
||||||
|
@ -417,6 +456,7 @@ function &get_categories($args = '') {
|
||||||
if ( count($excategories) ) {
|
if ( count($excategories) ) {
|
||||||
foreach ( $excategories as $excat ) {
|
foreach ( $excategories as $excat ) {
|
||||||
$exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
|
$exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
|
||||||
|
// TODO: Exclude children of excluded cats?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -435,6 +475,7 @@ function &get_categories($args = '') {
|
||||||
if ( empty($categories) )
|
if ( empty($categories) )
|
||||||
return array();
|
return array();
|
||||||
|
|
||||||
|
// TODO: Integrate this into the main query.
|
||||||
if ( $include_last_update_time ) {
|
if ( $include_last_update_time ) {
|
||||||
$stamps = $wpdb->get_results("SELECT category_id, UNIX_TIMESTAMP( MAX(post_date) ) AS ts FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories
|
$stamps = $wpdb->get_results("SELECT category_id, UNIX_TIMESTAMP( MAX(post_date) ) AS ts FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories
|
||||||
WHERE post_status = 'publish' AND post_id = ID AND $where GROUP BY category_id");
|
WHERE post_status = 'publish' AND post_id = ID AND $where GROUP BY category_id");
|
||||||
|
|
Loading…
Reference in New Issue