This page redirects to an external site: https://developer.wordpress.org/reference/functions/wp_list_categories/
Languages: English • 日本語 (Add your language)
Displays a list of Categories as links. When a Category link is clicked, all the posts in that Category will display on a Category Page using the appropriate Category Template dictated by the Template Hierarchy rules.
NOTE: wp_list_categories() works in much the same way as the two template tags replaced in WordPress 2.1, list_cats() and wp_list_cats() (both deprecated).
If you need a function that does not format the results, try get_categories()
<code style="color: #000000"> <span style="color: #0000BB"><?php wp_list_categories</span><span style="color: #007700">( </span><span style="color: #0000BB">$args </span><span style="color: #007700">); </span><span style="color: #0000BB">?></span> </code>
<?php
$args = array(
'show_option_all' => '',
'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' => '',
'include' => '',
'hierarchical' => 1,
'title_li' => __( 'Categories' ),
'show_option_none' => __( '' ),
'number' => null,
'echo' => 1,
'depth' => 0,
'current_category' => 0,
'pad_counts' => 0,
'taxonomy' => 'category',
'walker' => null
);
wp_list_categories( $args );
?>
By default, the usage shows:
This parameter added at Version 3.0 Valid values:
To sort categories alphabetically and include only the categories with IDs of 16, 3, 9 and 5, you could write the following code:
<ul>
<?php wp_list_categories('orderby=name&include=3,5,9,16'); ?>
</ul>
The following example displays category links sorted by name, shows the number of posts for each category, and excludes the category with the ID of 10 from the list.
<ul>
<?php wp_list_categories('orderby=name&show_count=1&exclude=10'); ?>
</ul>
The title_li parameter sets or hides a title or heading for the category list generated by wp_list_categories. It defaults to '(__('Categories')', i.e. it displays the word "Categories" as the list's heading. If the parameter is set to a null or empty value, no heading is displayed. The following example code excludes categories with IDs 4 and 7 and hides the list heading:
<ul>
<?php wp_list_categories('exclude=4,7&title_li='); ?>
</ul>
In the following example, only Categories with IDs 9, 5, and 23 are included in the list and the heading text has been changed to the word "Poetry", with a heading style of <h2>:
<ul>
<?php wp_list_categories('include=5,9,23&title_li=<h2>' . __('Poetry') . '</h2>' ); ?>
</ul>
The following example code generates category links, sorted by ID, only for the children of the category with ID 8; it shows the number of posts per category and hides category descriptions from the title attribute of the generated links. Note: If there are no posts in a parent Category, the parent Category will not display.
<ul>
<?php wp_list_categories('orderby=id&show_count=1&use_desc_for_title=0&child_of=8'); ?>
</ul>
When show_count=1, each category count is surrounded by parentheses. In order to remove the parentheses without modifying core WordPress files, use the following code.
<?php
$variable = wp_list_categories('echo=0&show_count=1&title_li=<h2>Categories</h2>');
$variable = preg_replace('~\((\d+)\)(?=\s*+<)~', '$1', $variable);
echo $variable;
?>
The following example generates Category links sorted by name, shows the number of posts per Category, and displays links to the RSS feed for each Category.
<ul>
<?php wp_list_categories('orderby=name&show_count=1&feed=RSS'); ?>
</ul>
To replace the rss link with a feed icon, you could write:
<ul>
<?php wp_list_categories('orderby=name&show_count=1&feed_image=/images/rss.gif'); ?>
</ul>
With Version 3.0 the taxonomy parameter was added to enable wp_list_categories() function to list Custom Taxonomies. This example list the terms in the taxonomy genre:
<?php //list terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin) $taxonomy = 'genre'; $orderby = 'name'; $show_count = 0; // 1 for yes, 0 for no $pad_counts = 0; // 1 for yes, 0 for no $hierarchical = 1; // 1 for yes, 0 for no $title = ''; $args = array( 'taxonomy' => $taxonomy, 'orderby' => $orderby, 'show_count' => $show_count, 'pad_counts' => $pad_counts, 'hierarchical' => $hierarchical, 'title_li' => $title ); ?> <ul> <?php wp_list_categories( $args ); ?> </ul>
Display the categories (or terms from other taxonomies) assigned to a post ordered by parent-child category relationship. Similar to the function get_the_category_list() which orders the categories by name. This example must be used inside the loop.
<?php
$taxonomy = 'category';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// display post categories
echo $terms;
}
?>
By default, wp_list_categories() generates nested unordered lists (ul) within a single list item (li) titled "Categories".
You can remove the outermost item and list by setting the title_li parameter to an empty string. You'll need to wrap the output in an ordered list (ol) or unordered list yourself (see the examples above). If you don't want list output at all, set the style parameter to none.
You can style the output with these CSS selectors :
li.categories { ... } /* outermost list item */
li.cat-item { ... }
li.cat-item-7 { ... } /* category ID #7, etc */
li.current-cat { ... }
li.current-cat-parent { ... }
ul.children { ... }
echo parameter.depth parameter.current_category parameter.exclude_tree parameter.pad_counts parameter.taxonomy parameter.show_last_update parameter.wp_list_categories() is located in wp-includes/category-template.php.