- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
「関数リファレンス/in category」の版間の差分
(en:Template Tags/in_category 07:55, 16 Apr 2007 Dspt 版) |
(和訳+編集/未訳あり:プラグインオプション) |
||
1行目: | 1行目: | ||
原文・最新版: [[:en:Template Tags/in_category|WordPress Codex » in_category]] | 原文・最新版: [[:en:Template Tags/in_category|WordPress Codex » in_category]] | ||
− | = | + | == 説明 == |
+ | (Description) | ||
− | + | 指定した[[用語集#Category|カテゴリ]]に現在の投稿が属するとき、true を返します。通常、このタグは[[:ja:The Loop|ループ]]の中で使いますが、[[#ループ外で使う|ループ外で使いたい場合]]は、事前に変数 <code>$post</code> の値をセットしておく必要があります。 | |
− | = | + | == 使い方 == |
− | + | (Usage) | |
+ | |||
+ | 例えば、現在処理中(ページを生成中)の投稿が、あるカテゴリID に属するときにのみ、特定の [[用語集#PHP|PHP]] や [[用語集#HTML|HTML]] を動作させたい場合には、これで 'category_id' を指定します。 | ||
<pre><?php if ( in_category('category_id') ): ?> | <pre><?php if ( in_category('category_id') ): ?> | ||
− | // | + | // ここにカテゴリ特有の PHP/HTML を書く |
<?php endif; ?></pre> | <?php endif; ?></pre> | ||
− | = | + | == 用例 == |
+ | (Examples) | ||
− | == Display Some Category Specific Text | + | === カテゴリ特有のテキストを表示 === |
− | + | (Display Some Category Specific Text) | |
+ | |||
+ | カテゴリ番号 '''5''' に属する各投稿に <code><nowiki><span class="good-cat-5">これはいいカテゴリです</span></nowiki></code> と表示し、それ以外の投稿には <code><nowiki><span class="bad-cat">これは悪いカテゴリです</span></nowiki></code> と表示: | ||
<pre><?php if ( in_category(5) ) { | <pre><?php if ( in_category(5) ) { | ||
− | echo '<span class="my-cat-5"> | + | echo '<span class="my-cat-5">これはいいカテゴリです</span>'; |
} else { | } else { | ||
− | echo '<span class="bad-cat"> | + | echo '<span class="bad-cat">これは悪いカテゴリです</span>'; |
} | } | ||
?></pre> | ?></pre> | ||
− | + | 残念ながら、<code>in_category</code> はカテゴリの親子関係を判断できません。例えばカテゴリ 11(バナナ)がカテゴリ 2(果物)の子カテゴリである場合に、バナナについての投稿を表示しているとき、<code>in_category('2')</code> は FALSE を返します。 | |
− | + | あるカテゴリとその全てのサブカテゴリに同じテキストを表示したいなら、その全てを書き出す必要があります。 | |
− | + | <code>in_category(2,11)</code> のような書き方はできません。PHP の式で <code>||</code>(論理和) <code>&&</code>(論理積) を使わなければなりません。(参考: [http://www.php.net/manual/ja/language.operators.php 演算子]) | |
− | <pre><?php if ( in_category(2) || in_category (11) || in_category (12)[ | + | |
− | echo '<span class="fruits"> | + | <pre><?php if ( in_category(2) || in_category (11) || in_category (12) [他にも果物のカテゴリがあれば - 面倒] ) { |
+ | echo '<span class="fruits">いろいろな種類の果物について</span>'; | ||
} else { | } else { | ||
− | echo '<span class="bad-cat"> | + | echo '<span class="bad-cat">おいしくない!健康的じゃない!</span>'; |
} | } | ||
?></pre> | ?></pre> | ||
− | == Use OUTSIDE The Loop | + | === ループ外で使う === |
+ | (Use OUTSIDE The Loop) | ||
− | + | 通常、このタグは[[The Loop|ループ]]の内側で使わなければなりません。ループ中でのみ値が与えられる WordPress の PHP 変数(<code>$post</code>)が必要だからです。しかし、この変数に''手動で''値を与え、このタグをちゃんと使えるようにもできます。 | |
− | + | 例えば[[Using Themes|テーマ]]内で、個々の投稿が属するカテゴリによって全く別のページを表示する <code>single.php</code> [[テンプレート|テンプレートファイル]]を使い分けたいとしましょう。<code>single.php</code> の中にあるループの中で <code>in_category()</code> を呼び出すのでは、都合が悪いですね。そこで、テーマの <code>single.php</code> を次のように使います。 | |
<pre><?php | <pre><?php | ||
49行目: | 57行目: | ||
?></pre> | ?></pre> | ||
− | + | カテゴリ '''17''' に属する投稿のときはテンプレートとして <code>single2.php</code> を使い、それ以外では <code>single1.php</code> を使います。 | |
− | == | + | == パラメータ == |
− | ; category_id : | + | (Parameters) |
− | :*< | + | |
− | :*< | + | ; category_id |
+ | : (整数) 検査したいカテゴリのカテゴリID。パタメータは、整数むき出しでも文字列として書いてもどちらでも構いません: | ||
+ | :*<code>in_category(5)</code> | ||
+ | :*<code>in_category('5')</code> | ||
+ | |||
+ | ==プラグインオプション== | ||
+ | (Plugin Options) | ||
− | |||
Eventually, someone will make a clever plugin that will do all of this automatically. At that point this example will become obsolete. However, the [http://boren.nu/downloads/custom_post_templates.phps Custom Post Templates Plugin] allows for creation of templates for single posts. It also shows an example of how to add a template which is used for ''all'' posts in a given category, not just a single post. That example is commented out in the plugin by default but can be easily implemented by uncommenting the appropriate lines. | Eventually, someone will make a clever plugin that will do all of this automatically. At that point this example will become obsolete. However, the [http://boren.nu/downloads/custom_post_templates.phps Custom Post Templates Plugin] allows for creation of templates for single posts. It also shows an example of how to add a template which is used for ''all'' posts in a given category, not just a single post. That example is commented out in the plugin by default but can be easily implemented by uncommenting the appropriate lines. | ||
− | == | + | == 外部資料 == |
+ | * [http://www.php.net/manual/ja/language.operators.php PHPマニュアル » 第15章 演算子] | ||
+ | |||
+ | == 関連 == | ||
+ | (Related) | ||
+ | |||
+ | {{DEFAULTSORT:in_category}} | ||
{{Tag Category Tags}} | {{Tag Category Tags}} | ||
{{PHP Function Tag Footer}} | {{PHP Function Tag Footer}} | ||
+ | |||
+ | [[en:Template Tags/in_category]] |
2007年7月20日 (金) 23:06時点における版
原文・最新版: WordPress Codex » in_category
説明
(Description)
指定したカテゴリに現在の投稿が属するとき、true を返します。通常、このタグはループの中で使いますが、ループ外で使いたい場合は、事前に変数 $post
の値をセットしておく必要があります。
使い方
(Usage)
例えば、現在処理中(ページを生成中)の投稿が、あるカテゴリID に属するときにのみ、特定の PHP や HTML を動作させたい場合には、これで 'category_id' を指定します。
<?php if ( in_category('category_id') ): ?> // ここにカテゴリ特有の PHP/HTML を書く <?php endif; ?>
用例
(Examples)
カテゴリ特有のテキストを表示
(Display Some Category Specific Text)
カテゴリ番号 5 に属する各投稿に <span class="good-cat-5">これはいいカテゴリです</span>
と表示し、それ以外の投稿には <span class="bad-cat">これは悪いカテゴリです</span>
と表示:
<?php if ( in_category(5) ) { echo '<span class="my-cat-5">これはいいカテゴリです</span>'; } else { echo '<span class="bad-cat">これは悪いカテゴリです</span>'; } ?>
残念ながら、in_category
はカテゴリの親子関係を判断できません。例えばカテゴリ 11(バナナ)がカテゴリ 2(果物)の子カテゴリである場合に、バナナについての投稿を表示しているとき、in_category('2')
は FALSE を返します。
あるカテゴリとその全てのサブカテゴリに同じテキストを表示したいなら、その全てを書き出す必要があります。
in_category(2,11)
のような書き方はできません。PHP の式で ||
(論理和) &&
(論理積) を使わなければなりません。(参考: 演算子)
<?php if ( in_category(2) || in_category (11) || in_category (12) [他にも果物のカテゴリがあれば - 面倒] ) { echo '<span class="fruits">いろいろな種類の果物について</span>'; } else { echo '<span class="bad-cat">おいしくない!健康的じゃない!</span>'; } ?>
ループ外で使う
(Use OUTSIDE The Loop)
通常、このタグはループの内側で使わなければなりません。ループ中でのみ値が与えられる WordPress の PHP 変数($post
)が必要だからです。しかし、この変数に手動で値を与え、このタグをちゃんと使えるようにもできます。
例えばテーマ内で、個々の投稿が属するカテゴリによって全く別のページを表示する single.php
テンプレートファイルを使い分けたいとしましょう。single.php
の中にあるループの中で in_category()
を呼び出すのでは、都合が悪いですね。そこで、テーマの single.php
を次のように使います。
<?php $post = $wp_query->post; if ( in_category('17') ) { include(TEMPLATEPATH . '/single2.php'); } else { include(TEMPLATEPATH . '/single1.php'); } ?>
カテゴリ 17 に属する投稿のときはテンプレートとして single2.php
を使い、それ以外では single1.php
を使います。
パラメータ
(Parameters)
- category_id
- (整数) 検査したいカテゴリのカテゴリID。パタメータは、整数むき出しでも文字列として書いてもどちらでも構いません:
in_category(5)
in_category('5')
プラグインオプション
(Plugin Options)
Eventually, someone will make a clever plugin that will do all of this automatically. At that point this example will become obsolete. However, the Custom Post Templates Plugin allows for creation of templates for single posts. It also shows an example of how to add a template which is used for all posts in a given category, not just a single post. That example is commented out in the plugin by default but can be easily implemented by uncommenting the appropriate lines.
外部資料
関連
(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()