wp_list_categories

Sponsored Link

リンクとしてカテゴリのリストを表示します。カテゴリーのリンクがクリックされる時、カテゴリーに属する全ての記事は、テンプレート階層の規則によって、カテゴリページに適切なカテゴリーテンプレートを使用して表示されます。

注:wp_list_categories()は、list_cats() and wp_list_cats()(非推奨)のWordPress 2.1で置き換えられた2つのテンプレートタグは、ほぼ同じ方法で動作をします。

Sponsored Link

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).

wp_list_categoriesのテンプレートタグ使用方法

 <?php wp_list_categories( $args ); ?>

デフォルトの使い方

<?php $args = 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_type'          => ,
    'feed_image'         => ,
    'exclude'            => ,
    'exclude_tree'       => ,
    'include'            => ,
    'current_category'   => 0,
    'hierarchical'       => true,
    'title_li'           => __( 'Categories' ),
    'number'             => NULL,
    'echo'               => 1,
    'depth'              => 0,
    'taxonomy'           => 'category' ); ?>

デフォルトでは、使用を示します:

By default, the usage shows:

  • すべてのカテゴリへのリンクがありません。
  • カテゴリー名を昇順にカテゴリのリストを並べ替えます。
  • 最後の更新を表示しない(各カテゴリーの最後の更新された投稿を表示しない)
  • 順不同リストのスタイルに表示します。
  • 記事のカウントを表示しません。
  • 記事のカテゴリーのみ表示
  • カテゴリーの説明をタイトル属性に設定します。
  • どんなCategoryの子供にも制限されません。
  • フィードやフィード画像を表示しません
  • 任意のカテゴリーを除外しないで、すべてのカテゴリを含む
  • CSSクラス接尾辞’current-cat’と有効なカテゴリーが表示されます
  • 階層を字下がりした方法でカテゴリーを表示する。
  • SQL LIMITを強制しない。(’number’ => 0 is not shown above)
  • カテゴリを(echoで)表示します。
  • 深さ制限なし。
  • 全てのカテゴリー

By default, the usage shows:

* No link to all categories
* Sorts the list of Categories 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 exclude any Category and includes all Categories
* Displays the active Category with the CSS Class-Suffix ‘ current-cat’
* 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)
* Displays (echos) the categories
* No limit to depth
* All categories.

wp_list_categoriesのパラメーター

show_option_all

(string) スタイルがリストに設定されている場合の非空白の値は、すべてのカテゴリへのリンクを表示します。デフォルト値はすべてのリンクを表示することはできません。

(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)カテゴリーはアルファベット順に、カテゴリーIDの特有によって、あるいはカテゴリーの記事数によってソートされます。デフォルトは、カテゴリ名でソートされます。有効な値:

(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
  • slug
  • count
  • term_group

order

(string) カテゴリの並べ替え順序(昇順または降順)。デフォルトは昇順です。有効な値:

(string) Sort order for categories (either ascending or descending). The default is ascending. Valid values:

  • ASC – default
  • DESC

show_last_updated

(boolean) 記事の最終更新のタイムスタンプが、(TRUE) か not (FALSE)を表示するでしょう。デフォルトはFALSEです。

(boolean) Should the last updated timestamp for posts be displayed (TRUE) or not (FALSE). Defaults to FALSE.

  • 1 (true)
  • 0 (false) – default

style

(string) スタイルは、カテゴリーリスト内に表示されます。リスト項目としてカテゴリーを表示するリストの値は、どれも特別な表示方法を生成します(リスト項目は
タグによって区切られます)。デフォルトの設定はリスト(順不同リストのリスト項目を作成します)です。詳細はマークアップセクションを参照してください。有効な値:

(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
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) 各カテゴリー内に現在の記事数を表示して切り替えます。デフォルトはfalse(記事数を表示しません)です。有効な値:

(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) 記事のないカテゴリーの表示を切り替えます。デフォルトは、true(空カテゴリーを隠します)です。有効な値:

(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
カテゴリーの説明に作成されたリンクのタイトル属性を挿入するかどうかを設定します(すなわち、Category Description” href=”…). The default is true (category descriptions will be inserted). Valid values:

  • 1 (true) – default
  • 0 (false)

child_of

(integer)このパラメーターによって識別された子カテゴリーのカテゴリーのみ表示します。このパラメーターのデフォルト値はありません。

(integer) Only display categories that are children of the category identified by this parameter. There is no default for this parameter.

feed

(string)各カテゴリーのRSS-2へのリンクとリンクテキスト表示するための設定を表示します。デフォルトでは、テキストもフィードも表示されません。

(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_type

(string)

feed_image

各カテゴリーのRSS-2フィードへのリンクとして機能するための画像(通常ではRSSフィードアイコン)のURIを設定します。このパラメーターは、フィードパラメーターを上書きします。このパラメーターのデフォルト値はありません。

(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) 結果から1つないし、複数のカテゴリーを除外します。このパラメーターは昇順に、ユニークIDによってコンマ区切りのカテゴリーリストを取得します。例を参照してください。child_of パラメーターは自動的にfalseに設定されます。

(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.

exclude_tree

結果からカテゴリーツリーを除外します。このパラメーターはバージョン2.7.1で追加されました。

(string) Exclude category-tree from the results. This parameter added at Version 2.7.1

include

(string)昇順で、ユニークIDによって区切られたリストに列挙されたカテゴリーのみ含みます。例を参照してください。

(string) Only include the categories detailed in a comma-separated list by unique ID, in ascending order. See the example.

hierarchical

(boolean) リスト項目内、あるいはインラインのようにサブカテゴリーを表示します。デフォルトはtrueです(親リスト項目の下にサブカテゴリーを表示します)。有効な値:

(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) タイトルと外側のリスト項目のスタイルを設定します。デフォルトは、”Categories”です。もし空が存在していた場合、外側のリスト項目は表示されません。後述の例を参照してください。

(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) カテゴリーの数を表示するために設定します。これは、SQLのLIMIT値を定義するようになります。デフォルトはLIMITです。

(integer) Sets the number of Categories to display. This causes the SQL LIMIT value to be defined. Default to no LIMIT.

echo

(boolean) 変数に保持するか結果を表示します。デフォルトはtrueです(整理されたカテゴリーを表示します)。このパラメーターはバージョン2.3から追加されました。有効な値:

(boolean) Show the result or keep it in a variable. The default is true (display the categories organized). Valid values:

  • 1 (true) – default
  • 0 (false)

depth

(integer)このパラメーターは、どのくらいのレベルのカテゴリーの階層をカテゴリーのリストに含むかを制御します。デフォルトは0です(全てのカテゴリーとそれらの子カテゴリーを表示します)。このパラメーターはバージョン2.5から追加されました。

  • 0 – すべてのカテゴリと子カテゴリ(デフォルト)。
  • -1 – 全てのカテゴリはフラット表示(インデントがない)フォーム(階層を無効にする)で表示します。
  • 1 – トップレベルのカテゴリーだけを表示します。
  • n – 値n(何らかの数)は、カテゴリーを表示する降下するための深さ(またはレベル)を指定します。

(integer) This parameter controls how many levels in the hierarchy of Categories are to be included in the list of Categories. The default value is 0 (display all Categories and their children). This parameter added at Version 2.5

  • 0 – All Categories and child Categories (Default).
  • -1 – All Categories displayed in flat (no indent) form (overrides hierarchical).
  • 1 – Show only top level Categories
  • n – Value of n (some number) specifies the depth (or level) to descend in displaying Categories

current_category

(integer)カテゴリーアーカイブページにないwp_list_categoriesの使用を表示するために”current-cat”を強制する事を許可します。通常、current-catはカテゴリアーカイブのページでのみ設定されます。もしそれに別の用途がある場合、または別のカテゴリーを強調するために強制したい場合は、”current”のカテゴリーを思考する関数を無効にします。
このパラメーターはバージョン2.6から追加されました。

(integer) Allows you to force the “current-cat” to appear on uses of wp_list_categories that are not on category archive pages. Normally, the current-cat is set only on category archive pages. If you have another use for it, or want to force it to highlight a different category, this overrides what the function thinks the “current” category is. This parameter added at Version 2.6

pad_counts

(boolean) 子カテゴリーから項目が含まれていることにより記事あるいはリンク数を計算します。もしshow_countsとhierarchicalがtrueの場合、自動的にtrueに設定されます。このパラメーターはバージョン2.9から追加されました。有効な値:

(boolean) Calculates link or post counts by including items from child categories. If show_counts and hierarchical are true this is automatically set to true. This parameter added at Version 2.9 Valid values:

  • 1 (true)
  • 0 (false) – default

taxonomy

(string) Taxonomyを返します。このパラメーターはバージョン3.0から追加されました。有効な値:

(string) Taxonomy to return. This parameter added at Version 3.0 Valid values:

  • category – default
  • taxonomy – or any registered taxonomy

カテゴリを含むか除く方法

あなたが次のコードを書く事で、16, 3, 9 そして 5のIDのカテゴリーのみ含めてアルファベット順にカテゴリーを並び替えます。

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&amp;include=3,5,9,16'); ?> 
</ul>

次の例は名前によってソートされたカテゴリーリンクを表示し、各カテゴリーの記事数を表示し、リストから10のIDを持つカテゴリーを除外します。

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&amp;show_count=1&amp;exclude=10'); ?> 
</ul>

リスト見出しを表示するか、または隠す方法

title_liパラメーターは、wp_list_categoriesによって生成されたカテゴリーリストのタイトルOR見出しを設定あるいは隠します。'(__(‘Categories’)’がデフォルトです、つまりリストの見出しとして”Categories”という事がが表示されます。もしパラメーターがnullあるいは空の値に設定されている場合、見出しは表示されません。次のコード例は、4と7のIDのカテゴリーを除外して、見出しリストを隠します。

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&amp;title_li='); ?>
</ul>

次の例では、9,5,23のIDに属するカテゴリーのみリスト内に含めて、見出しテキストは

の形式の見出しと共に”Poetry”という言葉に変更します。:

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

:

<ul>
<?php
wp_list_categories('include=5,9,23&amp;title_li=<h2>' . __('Poetry') . '</h2>' ); ?> 
</ul>

子カテゴリーのみ表示させる

次のコード例は、ID8の子カテゴリーのみのIDでソートされたカテゴリーリンクを生成します。;生成されたリンクのタイトル属性からカテゴリー説明を隠し、カテゴリー毎に記事数を表示します。注:親カテゴリーに記事がない場合、親カテゴリーは表示されません。

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&amp;show_count=1
&amp;use_desc_for_title=0&amp;child_of=8'); ?>
</ul>

カテゴリーカウントから括弧を削除する方法

show_count = 1とする時、各カテゴリー数は、括弧によって囲まれています。コアのWordPressファイルを変更せずに、括弧を削除するには、次のコードを使用します。

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&amp;show_count=1&amp;title_li=<h2>Categories</h2>');
$variable = str_replace(array('(',')'), '', $variable);
echo $variable;
?>

RSSフィードリンクと一緒にカテゴリーを表示させる

次の例では、カテゴリーリンクが名前によってソートされ、カテゴリーごとに記事数を表示し、各カテゴリー毎のRSSフィードへのリンクを表示します。

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&amp;show_count=1&amp;feed=RSS'); ?>
</ul>

フィードアイコンとRSSリンクを取り換えるためには、このように書く事が出来ます。

To replace the rss link with a feed icon, you could write:

<ul>
<?php 
wp_list_categories('orderby=name&amp;show_count=1
&amp;feed_image=/images/rss.gif'); ?>
</ul>

カスタムタクソノミーに条件を表示する方法

バージョン3.0と共に、タクソノミーパラメーターは、カスタムタクソノミーのリスト化するためのwp list categoriesを有効にするために追加しました。この例では、タクソノミーのジャンルに条件をリスト化します。

With Version 3.0 the taxonomy parameter was added to enable wp list categories 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)
$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
$taxonomy = 'genre';
$title = '';

$args = array(
  'orderby' => $orderby,
  'show_count' => $show_count,
  'pad_counts' => $pad_counts,
  'hierarchical' => $hierarchical,
  'taxonomy' => $taxonomy,
  'title_li' => $title
);
?>
<ul>
<?php 
wp_list_categories($args);
?>
</ul>

カテゴリリストをマークアップして、スタイリングする方法

デフォルトでは、wp_list_categories()は、”Categories”とタイトルを付けた単体のリスト項目(li)内に、入れ子にした順不同リスト(ul)を生成します。

一番外側の項目を削除して、title_liパラメーターを空の文字列に設定する事でリスト化する事が出来ます。あなた自身が(ol) の順序付きリストあるいは順不同リストの出力を囲む必要があります(以下の例を参照)。全てのリストを出力しない場合は、スタイルのパラメーターをなしに設定します。

CSSセレクターの出力でスタイルする事が出来ます:

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 below). 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 { ... }

変更ログ

* Since: 2.1.0
* 2.3.0 : Added the echo parameter.
* 2.5.0 : Added the depth parameter.
* 2.6.0 : Added the current_category parameter.
* 2.7.1 : Added the exclude_tree parameter.
* 2.9 : Added the pad_counts parameter.
* 3.0 : Added the taxonomy parameter.

ソースファイル

wp_list_categories()は、wp-includes/category-template.php内に位置しています。

wp_list_categories() is located in wp-includes/category-template.php.

関連テンプレートタグ

the_category, the_category_rss, single_cat_title, category_description, wp_dropdown_categories, wp_list_categories, get_the_category, get_category_parents, get_category_link, is_category, in_category , get_categories

wp_list_authors, wp_list_categories, wp_list_pages, wp_list_bookmarks, wp_list_comments, wp_get_archives, wp_page_menu, wp_dropdown_pages, wp_dropdown_categories, wp_dropdown_users

Sponsored Link