Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




wp_dropdown_categories [ WordPress Function ]

wp_dropdown_categories ( $args = '' )
Parameters:
  • (string|array) $args Optional. Override default arguments.
Returns:
  • (string) HTML content only if 'echo' argument is 0.
Defined at:



Display or retrieve the HTML dropdown list of categories.

The list of arguments is below: 'show_option_all' (string) - Text to display for showing all categories. 'show_option_none' (string) - Text to display for showing no categories. 'orderby' (string) default is 'ID' - What column to use for ordering the categories. 'order' (string) default is 'ASC' - What direction to order categories. 'show_count' (bool|int) default is 0 - Whether to show how many posts are in the category. 'hide_empty' (bool|int) default is 1 - Whether to hide categories that don't have any posts attached to them. 'child_of' (int) default is 0 - See {@link get_categories()}. 'exclude' (string) - See {@link get_categories()}. 'echo' (bool|int) default is 1 - Whether to display or retrieve content. 'depth' (int) - The max depth. 'tab_index' (int) - Tab index for select element. 'name' (string) - The name attribute value for select element. 'id' (string) - The ID attribute value for select element. Defaults to name if omitted. 'class' (string) - The class attribute value for select element. 'selected' (int) - Which category ID is selected. 'taxonomy' (string) - The name of the taxonomy to retrieve. Defaults to category.

The 'hierarchical' argument, which is disabled by default, will override the depth argument, unless it is true. When the argument is false, it will display all of the categories. When it is enabled it will use the value in the 'depth' argument.

Source


<?php
function wp_dropdown_categories$args '' ) {
    
$defaults = array(
        
'show_option_all' => '''show_option_none' => '',
        
'orderby' => 'id''order' => 'ASC',
        
'show_count' => 0,
        
'hide_empty' => 1'child_of' => 0,
        
'exclude' => '''echo' => 1,
        
'selected' => 0'hierarchical' => 0,
        
'name' => 'cat''id' => '',
        
'class' => 'postform''depth' => 0,
        
'tab_index' => 0'taxonomy' => 'category',
        
'hide_if_empty' => false
    
);

    
$defaults['selected'] = ( is_category() ) ? get_query_var'cat' ) : 0;

    
// Back compat.
    
if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
        
_deprecated_argument__FUNCTION__'3.0''' );
        
$args['taxonomy'] = 'link_category';
    }

    
$r wp_parse_args$args$defaults );

    if ( !isset( 
$r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
        
$r['pad_counts'] = true;
    }

    
extract$r );

    
$tab_index_attribute '';
    if ( (int) 
$tab_index )
        
$tab_index_attribute " tabindex=\"$tab_index\"";

    
$categories get_terms$taxonomy$r );
    
$name esc_attr$name );
    
$class esc_attr$class );
    
$id $id esc_attr$id ) : $name;

    if ( ! 
$r['hide_if_empty'] || ! empty($categories) )
        
$output "<select name='$name' id='$id' class='$class$tab_index_attribute>\n";
    else
        
$output '';

    if ( empty(
$categories) && ! $r['hide_if_empty'] && !empty($show_option_none) ) {
        
$show_option_none apply_filters'list_cats'$show_option_none );
        
$output .= "\t<option value='-1' selected='selected'>$show_option_none</option>\n";
    }

    if ( ! empty( 
$categories ) ) {

        if ( 
$show_option_all ) {
            
$show_option_all apply_filters'list_cats'$show_option_all );
            
$selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" '';
            
$output .= "\t<option value='0'$selected>$show_option_all</option>\n";
        }

        if ( 
$show_option_none ) {
            
$show_option_none apply_filters'list_cats'$show_option_none );
            
$selected = ( '-1' === strval($r['selected']) ) ? " selected='selected'" '';
            
$output .= "\t<option value='-1'$selected>$show_option_none</option>\n";
        }

        if ( 
$hierarchical )
            
$depth $r['depth'];  // Walk the full depth.
        
else
            
$depth = -1// Flat.

        
$output .= walk_category_dropdown_tree$categories$depth$r );
    }

    if ( ! 
$r['hide_if_empty'] || ! empty($categories) )
        
$output .= "</select>\n";

    
$output apply_filters'wp_dropdown_cats'$output );

    if ( 
$echo )
        echo 
$output;

    return 
$output;
}
?>

Examples [ wp-snippets.com ]

Top Google zoekresultaten

Meer ...

0 User Note(s)

Nog geen één. Wees de eerste!

Nieuw toevoegen ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics