• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

テンプレートタグ/wp list categories

提供: WordPress Codex 日本語版
< テンプレートタグ
2007年7月18日 (水) 20:43時点におけるBono (トーク | 投稿記録)による版 (en:Template Tags/wp_list_categories 18:16, 20 Jun 2007 MichaelH 版)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

Description

The template tag, wp_list_categories, 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. wp_list_categories works in much the same way as the two template tags replaced in WordPress 2.1, list_cats()(deprecated), and wp_list_cats()(deprecated).

Usage

%%% <?php wp_list_categories('arguments'); ?> %%%

Examples

Default Usage

$defaults = array('show_option_all' => '', 'orderby' => 'name', 'order' => 'ASC', 
'show_last_update' => 0, 'style' => 'list','show_count' => 0, 'hide_empty' => 1, 
'use_desc_for_title' => 1, 'child_of' => 0, 'feed' => '', 'feed_image' => '', 
'exclude' => '', 'hierarchical' => true, 'title_li' => __('Categories'));

By default, the usage shows:

  • No link to all categories
  • Sorts the list of Caegories in by the Category name in ascending order
  • Does not show the last update (last updated post in each Category)
  • Displayed in an unordered list style
  • Does not show the post count
  • Displays only Categories with posts
  • Sets the title attribute to the Category Description
  • Is not restricted to the child_of any Category
  • No feed or feed image used
  • Does not exlude any Category and includes all Categories ('include' => is not shown above)
  • Shows the Categories in hierarchical indented fashion
  • Display Category as the heading over the list
  • No SQL LIMIT is imposed ('number' => 0 is not shown above)
wp_list_categories();

Include or Exclude Categories

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>

Display or Hide the List Heading

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 Cateogories 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>

Only Show Children of a Category

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>

Display Categories with RSS Feed Links

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>

Markup and Styling of Category Lists

By default, wp_list_categories() generates list items <li> for an unordered list <ul>. All list items generated by wp_list_categories() are marked with the class "page_item". When wp_list_categories() is called while displaying a Category Page, the list item for that Page will be given the additional class "current_page_item":

<ul>
   <li class="page_item current_page_item">
     [the current page]
   </li>
   <li class="page_item">
     [another page]
   </li>
</ul>

You can style these classes with CSS selectors :

 .page_item { ... }
 .current_page_item { ... }

Note: The list items generated by wp_list_categories (unless the style parameter is set to none) require an opening and closing <ul> tag. See the examples below.


Parameters

show_option_all 
(string) A non-blank values causes the display of a link to all categories if the style is set to list. The default value is not to display a link to all.
orderby 
(string) Sort categories alphabetically, by unique Category ID, or by the count of posts in that Category. The default is sort by category name. Valid values:
  • ID
  • name - default
  • count
order 
(string) Sort order for categories (either ascending or descending). The default is ascending. Valid values:
  • ASC - default
  • DESC
show_last_updated 
(boolean) Should the last updated timestamp for posts be displayed (TRUE) or not (FALSE). Defaults to FALSE.
* 1 (true)
* 0 (false) - default
style 
(string) Style to display the categories list in. A value of list displays the categories as list items while none generates no special display method (the list items are separated by <br> tags). The default setting is list (creates list items for an unordered list). See the markup section for more. Valid values:
  • list - default.
  • none
show_count 
(boolean) Toggles the display of the current count of posts in each category. The default is false (do not show post counts). Valid values:
  • 1 (true)
  • 0 (false) - default
hide_empty 
(boolean) Toggles the display of categories with no posts. The default is true (hide empty categories). Valid values:
  • 1 (true) - default
  • 0 (false)
use_desc_for_title 
(boolean) Sets whether a category's description is inserted into the title attribute of the links created (i.e. <a title="<em>Category Description</em>" href="...). The default is true (category descriptions will be inserted). Valid values:
  • 1 (true) - default
  • 0 (false)
child_of 
(integer) Only display categories that are children of the category identified by this parameter. There is no default for this parameter. If the parameter is used, the hide_empty parameter is set to false.
feed (string)
Display a link to each category's rss-2 feed and set the link text to display. The default is no text and no feed displayed.
feed_image 
(string) Set a URI for an image (usually an rss feed icon) to act as a link to each categories' rss-2 feed. This parameter overrides the feed parameter. There is no default for this parameter.
exclude 
(string) Exclude one or more categories from the results. This parameter takes a comma-separated list of categories by unique ID, in ascending order. See the example. The child_of parameter is automatically set to false.
include 
(string) Only include the categories detailed in a comma-separated list by unique ID, in ascending order. See the example.
hierarchical 
(boolean) Display sub-categories as inner list items (below the parent list item) or inline. The default is true (display sub-categories below the parent list item). Valid values:
  • 1 (true) - default
  • 0 (false)
title_li 
(string) Set the title and style of the outer list item. Defaults to "_Categories". If present but empty, the outer list item will not be displayed. See below for examples.
number 
(integer) Sets the number of Categories to display. This causes the SQL LIMIT value to be defined. Default to no LIMIT.

Related

カテゴリータグ: the_category(), the_category_rss(), single_cat_title(), category_description(), wp_dropdown_categories(), wp_list_categories(), get_the_category(), get_the_category_by_ID(), get_category_by_slug(), get_the_category_list(), get_category_parents(), get_category_link(), is_category(), in_category()


テンプレートタグへのクエリ文字列型パラメータの渡し方関数リファレンステンプレートタグ目次もご覧ください。


この記事は翻訳時に編集が必要であるとマークされていました。その為Codex原文が大きく編集されている可能性があります。内容を確認される際は原文を参照していただき、可能であれば本項目へ反映させてください。よりよいCodexを作成するためのお手伝いをお願いします。