wp_dropdown_categories

Sponsored Link

送信ボタンがないセレクト(つまり、ドロップダウン)ボックスにカテゴリーリストを表示します。

Sponsored Link

Displays a list of categories in a select (i.e dropdown) box with no submit button.

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

<?php wp_dropdown_categories( $args ); ?> 

デフォルトの使い方

<?php $args = array(
    'show_option_all'    => ,
    'show_option_none'   => ,
    'orderby'            => 'ID', 
    'order'              => 'ASC',
    'show_last_update'   => 0,
    'show_count'         => 0,
    'hide_empty'         => 1, 
    'child_of'           => 0,
    'exclude'            => ,
    'echo'               => 1,
    'selected'           => 0,
    'hierarchical'       => 0, 
    'name'               => 'cat',
    'class'              => 'postform',
    'depth'              => 0 ); ?> 

デフォルトでの、使用を表示:

  • 昇順のカテゴリーidでソート
  • 最終更新日を表示しない
  • カテゴリ内の記事数を表示しない
  • ‘空’のカテゴリを表示しません
  • 何も除外しない
  • カテゴリーを(エコー)として表示
  • カテゴリーがないものをフォーム’selected’する
  • 階層構造のカテゴリを表示しない
  • フォーム名に’cat’を割り当てます
  • フォームのclassに’postform’を割り当てます
  • 深さの制限をしない

By default, the usage shows:

* Sorts by category id in ascending order
* Does not show the last date updated
* Does not show the count of posts within a category
* Does not show ‘empty’ categories
* Excludes nothing
* Displays (echos) the categories
* No category is ‘selected’ in the form
* Does not display the categories in a hierarchical structure
* Assigns ‘cat’ to the form name
* Assigns the form to the class ‘postform’
* No depth limit

wp_dropdown_categoriesのパラメーター

show_option_all

(string)すべてのカテゴリを選択できるようにするドロップダウンのためのHTMLケースです。

(string) Causes the HTML for the dropdown to allow you to select All of the categories.

show_option_none

(string)カテゴリーのない選択をできるようにするドロップダウンのためのHTMLのケースです。

(string) Causes the HTML for the dropdown to allow you to select NONE of the categories.

orderby

(string)オプションをソートするキー。有効な値:

(string) Key to sort options by. Valid values:

  • ‘ID’ (Default)
  • ‘name’

order

(string)オプションの並べ替え順。有効な値:

(string) Sort order for options. Valid values:

  • ‘ASC’ (Default)
  • ‘DESC’

show_last_update

(boolean) 各カテゴリーの最終投稿日を表示するどうかを設定します。有効な値:

(boolean) Sets whether to display the date of the last post in each category. Valid values:

  • 1 (True)
  • 0 (False – Default)

show_count

(boolean)各カテゴリーの投稿数を表示するかどうかを設定します。有効な値:

(boolean) Sets whether to display a count of posts in each category. Valid values:

  • 1 (True)
  • 0 (False – Default)

hide_empty

(boolean) 記事がないカテゴリーを非表示(表示しない)にするどうかを設定します。有効な値:

(boolean) Sets whether to hide (not display) categories with no posts. Valid values:

  • 1 (True – Default)
  • 0 (False)

child_of

(integer)カテゴリーIDで識別されるの子カテゴリーがあるカテゴリーのみを表示します。このパラメータのデフォルト値はありません。

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

exclude

(string)カンマで区切られたカテゴリIDのリストを除外します。たとえば、’exclude=4,12’の意味IDは、カテゴリーID4と12は、表示/エコーか返しません。デフォルトは何も除外しません。

(string) Comma separated list of category IDs to exclude. For example, ‘exclude=4,12’ means category IDs 4 and 12 will NOT be displayed/echoed or returned. Defaults to exclude nothing.

exclude_tree

(string)”親”カテゴリーIDのカンマ区切りのリストを除外することを定義します。親と親の子カテゴリー全てを除外する時、このパラメータを使用します。だから’exclude_tree=5’は、親カテゴリー5とその子(すべての子孫)カテゴリーを除外します。該当する階層がtrueの場合は、exclude_treeは値を除外して使います。このパラメーターは、バージョン2.7で利用できました。

(string) Define a comma-separated list of “parent” category IDs to be excluded. Use this parameter to exclude a parent and all of that parent’s child categories. So ‘exclude_tree=5’ would exclude the parent category 5, and its child (all descendant) categories. If hierarchical is true, exclude_tree uses the exclude value. This parameter was available at Version 2.7.1.

echo

(boolean) カテゴリー(TRUE)を表示、またはPHP(FALSE)を使用するためにそれらを返します。デフォルトはTRUE。

(boolean) Display bookmarks (TRUE) or return them for use by PHP (FALSE). Defaults to TRUE.

  • 1 (True – default)
  • 0 (False)

selected

(integer)カテゴリーのカテゴリーIDは表示ボックスに’選択’または提示されます。デフォルトは、ないカテゴリーを選択。

(integer) Category ID of the category to be ‘selected’ or presented in the display box. Defaults to no category selected.

hierarchical

(boolean) 階層的にカテゴリーを表示します(子カテゴリはインデントを表示)。デフォルトはFALSEです。

(boolean) Display categories in hierarchical fashion (child categories show indented). Defaults to FALSE.

  • 1 (True)
  • 0 (False – Default)

name

(string)ドロップダウンフォームに割り当てられた名前です。デフォルトは、’cat’。

(string) Name assigned to the dropdown form. Defaults to ‘cat’.

class

(string)ドロップダウンフォーム割り当てられるClassです。デフォルトは’postform’です。

(string) Class assigned to the dropdown form. Defaults to ‘postform’.

depth

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

  • 0 – 全てのカテゴリーと子カテゴリー(デフォルト)。
  • -1 – 全てのカテゴリーは、フラット(インデントがない)フォーム(オーバーライト階層)で表示されます。
  • 1 – トップレベルのカテゴリーのみ表示
  • 値2(何らかの数)は、表示しているページに降りていくための深さ(またはレベル)を指定します。表示しているカテゴリーに伝わっていた

  • 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

pad_counts

(boolean) 子カテゴリーから項目を含めることにより、リンク或いは投稿のカウントを計算します。show_countsと階層が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

送信ボタンと一緒にドロップダウンを表示する方法

送信ボタンを持つHTML形式で階層カテゴリーのドロップダウンリストを、WordPressのサイドバーに順不同リストに、各カテゴリー内の投稿数と一緒に表示します。

Displays a hierarchical category dropdown list in HTML form with a submit button, in a WordPress sidebar unordered list, with a count of posts in each category.

<li id="categories">
	<h2>< ?php _e('Categories:'); ?></h2>
	<form action="<?php bloginfo('url'); ?>" method="get">
	<div>
	< ?php wp_dropdown_categories('show_count=1&amp;hierarchical=1'); ?>
	<input type="submit" name="submit" value="view" />
	</div>
	</form>
</li>

JavaScriptを使用して、送信ボタンなしでドロップダウンを表示する方法

例では、show_option_none パラメーターを使用してすることで表現し、Moshu’s のフォーラムのポストから収集されました。

Example depicts using the show_option_none parameter and was gleaned from Moshu’s forum post.

<li id="categories"><h2>< ?php _e('Posts by Category'); ?></h2>
	< ?php wp_dropdown_categories('show_option_none=Select category'); ?>

<script type="text/javascript"><!--
    var dropdown = document.getElementById("cat");
    function onCatChange() {
		if ( dropdown.options&#91;dropdown.selectedIndex&#93;.value > 0 ) {
			location.href = "< ?php echo get_option('home');
?>/?cat="+dropdown.options&#91;dropdown.selectedIndex&#93;.value;
		}
    }
    dropdown.onchange = onCatChange;
--></script>
</li>

JavaScriptを使用して、送信ボタンなしでドロップダウンを表示する方法(2)

この例では、echoパラメーター(echo=0)を使用しました。シンプルなpreg_replaceのJavaScriptコードを挿入します。それも、JavaScriptなしで動作します(送信ボタンは、noscriptタグによって囲まれます)。

In this example the echo parameter (echo=0) is used. A simple preg_replace inserts the JavaScript code. It even works without JavaScript (submit button is wrapped by noscript tags).

<li id="categories">
	<h2>< ?php _e('Posts by Category'); ?></h2>
	<form action="<?php bloginfo('url'); ?>/" method="get">
	<div>
< ?php
$select = wp_dropdown_categories('show_option_none=Select category&amp;show_count=1&amp;orderby=name&amp;echo=0');
$select = preg_replace("#<select(&#91;^>]*)>#", "<select $1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
	<noscript><div><input type="submit" value="View" /></div></noscript>
	</select></div></form>
</li>

変更ログ

Since: 2.1.0

ソースファイル

wp_dropdown_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

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