get_categories

Sponsored Link

クエリパラメータに一致するカテゴリーオブジェクトの配列を返します。

Sponsored Link

Returns an array of category objects matching the query parameters.

引数はwp_list_categoriesとほぼ同一であり、配列あるいはクエリ構文で渡すことができます。

Arguments are pretty much the same as wp_list_categories and can be passed as either array or in query syntax.

get_categoriesのファンクションタグ使用方法

<?php $categories = get_categories( $args ); ?>

デフォルトの使用方法

<?php $args = array(
	'type'                     => 'post',
	'child_of'                 => 0,
	'parent'                   => '',
	'orderby'                  => 'name',
	'order'                    => 'ASC',
	'hide_empty'               => 1,
	'hierarchical'             => 1,
	'exclude'                  => '',
	'include'                  => '',
	'number'                   => '',
	'taxonomy'                 => 'category',
	'pad_counts'               => false );?>

get_categoriesのパラメーター

type

(string) 取得するためのカテゴリーのタイプ

(string) Type of category to retrieve

  • post – default
  • link

注意:type=linkはWordPress3.0以降で廃止されました。代わりにtaxonomy=link_categoryを使用しています。

Note: type=link has been deprecated from WordPress 3.0 onwards. Use taxonomy=link_category instead.

child_of

(integer)IDによって識別されたカテゴリーの子孫(すなわち子&孫)であるすべてのカテゴリーが表示されます。このパラメータにデフォルト値はありません。パラメータが使用されている場合、hide_emptyパラメータがfalseに設定されています。

(integer) Display all categories that are descendants (i.e. children & grandchildren) of the category identified by its ID. There is no default for this parameter. If the parameter is used, the hide_empty parameter is set to false.

parent

(integer) IDで識別されるカテゴリーの直接の子孫(すなわち子のみ)であるカテゴリーだけを表示します。これは ‘child_of’パラメータのように動作しません。このパラメータのデフォルト値はありません。 [2.8.4 IN]

(integer) Display only categories that are direct descendants (i.e. children only) of the category identified by its ID. This does NOT work like the ‘child_of’ parameter. There is no default for this parameter. [In 2.8.4]

orderby

(string) ユニークなカテゴリーIDまたはカテゴリー順にソートします。デフォルトでは、カテゴリーIDでソートしています。有効な値:

(string) Sort categories alphabetically or by unique category ID. The default is sort by Category ID. 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

hide_empty

(boolean)投稿がないカテゴリーの表示を切り替えます。デフォルトはtrueの場合は1、またはfalseの場合(空のカテゴリーを表示する場合)は’0’を追加することができます。有効な値:

(boolean) Toggles the display of categories with no posts. The default is 1 for true or you can add ‘0’ for false (show empty categories). Valid values:

  • 1 – default
  • 0

hierarchical

(boolean)trueの場合、結果は空でないサブカテゴリーを持つサブカテゴリーがある限り、空であるサブカテゴリーを含みます。デフォルトはtrueです。有効な値:

(boolean) When true, the results will include sub-categories that are empty, as long as those sub-categories have sub-categories that are not empty. The default is true. Valid values:

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

exclude

(string)wp_list_categoriesによって生成されたリストから1つまたは複数のカテゴリーを除外します。このパラメーターは、ユニークIDによってカテゴリーのカンマによって区切られたリストを取得します。例を参照してください。

(string) Excludes one or more categories from the list generated by wp_list_categories. This parameter takes a comma-separated list of categories by unique ID, in ascending order. See the example.

include

(string)wp_list_categoriesによって生成されたリスト内の特定のカテゴリーのみ含みます。このパラメーターは、昇順で、ユニークIDによってカテゴリーのカンマで区切られたリストを取得します。例を参照してください。

(string) Only include certain categories in the list generated by wp_list_categories. This parameter takes a comma-separated list of categories by unique ID, in ascending order. See the example.

  • list – default.
  • none

number

(string)カテゴリー数を返します。

(string) The number of categories to return

taxonomy

(string or array)タクソノミーを返します。このパラメーターは、バージョン3.0で有効な値を追加しました:

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

  • category – default
  • taxonomy – or any registered taxonomy

pad_counts

(boolean)子カテゴリーからアイテムをインクルードすることによって、リンクや投稿数を計算します。有効な値:

(boolean) Calculates link or post counts by including items from child categories. Valid values:

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

戻り値

(array) クエリパラメーターを一致するカテゴリーオブジェクトの配列を返します。

(array)
Returns an array of category objects matching the query parameters.

投稿カテゴリーページで親カテゴリー内で使用されているドロップダウンボックス

これは、カテゴリページのビルドに使用されているコードです。 3.0.1からのコードです。

This is the code used in the build in category page. Code from 3.0.1

wp_dropdown_categories(array('hide_empty' => 0, 'name' => 'category_parent', 'orderby' => 'name', 'selected' => $category->parent, 'hierarchical' => true, 'show_option_none' => __('None')));

このわずかに変更しているコードは、全てのカテゴリーを取得し、新しいレベル(子カテゴリー)のインデントでそれらを表示します。選択ボックスには’select_name’と呼ばれるname=とid=があります。
元のコードとしてデフォルトの”none”を表示しないこの選択は、別のカテゴリー(或いはない)の子としてカテゴリーを添付するために使用されます。

This slightly altered code will grab all categories and display them with indent for a new level (child category). The select box will have a name= and id= called ‘select_name’. This select will not display a default “none” as the original code was used to attach a category as a child to another category (or none).

wp_dropdown_categories(array('hide_empty' => 0, 'name' => 'select_name', 'hierarchical' => true));

ドロップダウンボックス

ここでは過去のイベント上のアーカイブ情報であるカテゴリーをいい、サブカテゴリーのドロップダウンボックスを作成する方法です。これは月別アーカイブのドロップダウンを作成する方法を表示しているwp_get_archivesのドロップダウン例の一例を反映しています。

表示したいサブカテゴリーはカテゴリー10であるカテゴリーと仮定して、そのカテゴリーである”nicename”カテゴリーは “archives”です。

Here’s how to create a dropdown box of the subcategories of, say, a category that archives information on past events. This mirrors the example of the dropdown example of wp_get_archives which shows how to create a dropdown box for monthly archives.

Suppose the category whose subcategories you want to show is category 10, and that its category “nicename” is “archives”.

<select name="event-dropdown" onchange='document.location.href=this.options&#91;this.selectedIndex&#93;.value;'> 
 <option value=""><?php echo esc_attr(__('Select Event')); ?></option> 
 <?php 
  $categories=  get_categories('child_of=10'); 
  foreach ($categories as $category) {
  	$option = '<option value="/category/archives/'.$category->category_nicename.'">';
	$option .= $category->cat_name;
	$option .= ' ('.$category->category_count.')';
	$option .= '</option>';
	echo $option;
  }
 ?>
</select>

カテゴリーと説明文をリスト化する方法

この例では、アルファベット順に一覧表示され、すべてのカテゴリーには、対応しているカテゴリーアーカイブへのリンクとして提示します。各カテゴリーの説明文はカテゴリーリンクの後に表示されます。

This example will list in alphabetic order, all categories presented as links to the corresponding category archive. Each category descripition is listed after the category link.

<?php
$args=array(
  'orderby' => 'name',
  'order' => 'ASC'
  );
$categories=get_categories($args);
  foreach($categories as $category) { 
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    echo '<p> 説明文:'. $category->description . '</p>';
    echo '<p> 投稿数: '. $category->count . '</p>';  } 
?>

$categoryの全ての内容は次のとおりです:

The complete content of $category is:

$category->term_id
$category->name
$category->slug
$category->term_group
$category->term_taxonomy_id
$category->taxonomy
$category->description
$category->parent
$category->count
$category->cat_ID
$category->category_count
$category->category_description
$category->cat_name
$category->category_nicename
$category->category_parent

ソースファイル

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

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

Sponsored Link