wp_list_categories( array|string $args = '' )
显示或检索类别的HTML列表。
Display or retrieve the HTML list of categories.
源码(Source)
/**
* Display or retrieve the HTML list of categories.
*
* The list of arguments is below:
* 'show_option_all' (string) - Text to display for showing all 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.
* 'use_desc_for_title' (bool|int) default is 1 - Whether to use the
* category description as the title attribute.
* 'feed' - See {@link get_categories()}.
* 'feed_type' - See {@link get_categories()}.
* 'feed_image' - See {@link get_categories()}.
* 'child_of' (int) default is 0 - See {@link get_categories()}.
* 'exclude' (string) - See {@link get_categories()}.
* 'exclude_tree' (string) - See {@link get_categories()}.
* 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
* 'current_category' (int) - See {@link get_categories()}.
* 'hierarchical' (bool) - See {@link get_categories()}.
* 'title_li' (string) - See {@link get_categories()}.
* 'depth' (int) - The max depth.
*
* @since 2.1.0
*
* @param string|array $args Optional. Override default arguments.
* @return false|string HTML content only if 'echo' argument is 0.
*/
function wp_list_categories( $args = '' ) {
$defaults = array(
'show_option_all' => '', 'show_option_none' => __('No categories'),
'orderby' => 'name', 'order' => 'ASC',
'style' => 'list',
'show_count' => 0, 'hide_empty' => 1,
'use_desc_for_title' => 1, 'child_of' => 0,
'feed' => '', 'feed_type' => '',
'feed_image' => '', 'exclude' => '',
'exclude_tree' => '', 'current_category' => 0,
'hierarchical' => true, 'title_li' => __( 'Categories' ),
'echo' => 1, 'depth' => 0,
'taxonomy' => 'category'
);
$r = wp_parse_args( $args, $defaults );
if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
$r['pad_counts'] = true;
if ( true == $r['hierarchical'] ) {
$r['exclude_tree'] = $r['exclude'];
$r['exclude'] = '';
}
if ( ! isset( $r['class'] ) )
$r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
if ( ! taxonomy_exists( $r['taxonomy'] ) ) {
return false;
}
$show_option_all = $r['show_option_all'];
$show_option_none = $r['show_option_none'];
$categories = get_categories( $r );
$output = '';
if ( $r['title_li'] && 'list' == $r['style'] ) {
$output = '' . $r['title_li'] . '';
}
if ( empty( $categories ) ) {
if ( ! empty( $show_option_none ) ) {
if ( 'list' == $r['style'] ) {
$output .= '' . $show_option_none . '';
} else {
$output .= $show_option_none;
}
}
} else {
if ( ! empty( $show_option_all ) ) {
$posts_page = '';
// For taxonomies that belong only to custom post types, point to a valid archive.
$taxonomy_object = get_taxonomy( $r['taxonomy'] );
if ( ! in_array( 'post', $taxonomy_object->object_type ) && ! in_array( 'page', $taxonomy_object->object_type ) ) {
foreach ( $taxonomy_object->object_type as $object_type ) {
$_object_type = get_post_type_object( $object_type );
// Grab the first one.
if ( ! empty( $_object_type->has_archive ) ) {
$posts_page = get_post_type_archive_link( $object_type );
break;
}
}
}
// Fallback for the 'All' link is the front page.
if ( ! $posts_page ) {
$posts_page = 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ? get_permalink( get_option( 'page_for_posts' ) ) : home_url( '/' );
}
$posts_page = esc_url( $posts_page );
if ( 'list' == $r['style'] ) {
$output .= "$show_option_all";
} else {
$output .= "$show_option_all";
}
}
if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
$current_term_object = get_queried_object();
if ( $current_term_object && $r['taxonomy'] === $current_term_object->taxonomy ) {
$r['current_category'] = get_queried_object_id();
}
}
if ( $r['hierarchical'] ) {
$depth = $r['depth'];
} else {
$depth = -1; // Flat.
}
$output .= walk_category_tree( $categories, $depth, $r );
}
if ( $r['title_li'] && 'list' == $r['style'] )
$output .= '';
/**
* Filter the HTML output of a taxonomy list.
*
* @since 2.1.0
*
* @param string $output HTML output.
* @param array $args An array of taxonomy-listing arguments.
*/
$html = apply_filters( 'wp_list_categories', $output, $args );
if ( $r['echo'] ) {
echo $html;
} else {
return $html;
}
}
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
4.4.0 | wp-includes/category-template.php | 9 | 19 |