in_category – WordPress*リファレンス
本当に、現在のポストが指定されたCategoryにあるなら、戻ります。 通常、このタグはLoopの中で使用されますが、輪の外でこのタグを使用するとき、$postを設定しなければなりません。
⇒Returns true if the current post is in the specified Category. Normally this tag is used within The Loop, but the $post variable must be set when using this tag outside of the loop.
in_categoryのテンプレートタグ使用方法
処理されている現在の地位が我々が『category_id』としてここで代表するカテゴリーID番号でカテゴリーでいる場合だけ、あなたが若干の特定のPHPまたはHTMLを実行したいと思ってください。
⇒Suppose you want to execute some specific PHP or HTML only if the current post being processed is in a category with a category ID number we’ll represent here as ‘category_id’.
< ?php if ( in_category('category_id') ): ?>
// Some category specific PHP/HTML here
< ?php endif; ?>
in_categoryのデフォルト用法
何らかのカテゴリの特定のテキストを表示してください。
カテゴリー5のメンバーである各々の地位の表示<span class=”good-cat-5″>This is a nice category</spanは、さもなければ<span class=”bad-cat”>This is a BAD category</span>を示します
⇒Display <span class=”good-cat-5″>This is a nice category</span> in each post which is a member of category 5, otherwise display <span class=”bad-cat”>This is a BAD category</span>.
< ?php if ( in_category(5) ) {
echo 'This is a nice category';
} else {
echo 'This is a BAD category';
}
?>
残念なことに、in_categoryはカテゴリー親子関係を理解しません。たとえば、カテゴリー11(バナナ)がカテゴリー2(果物)の子供であるならば、バナナについてポストを見るとき、in_category(2)はFALSEを返します。あなたが同じテキストがそうであることを望むもしもがすべてのそのサブカテゴリをカテゴリーANDに適用したように、あなたは彼ら全員をリストしなければなりません。in_category(2,11)のような構文は、許されません。あなたは、表現においてPHP || (logical OR) && (logical AND)を使わなければなりません。
⇒Unfortunately, in_category doesn’t understand category child-parent relationships. If, for example, category 11 (bananas) is a child of category 2 (fruits), in_category(‘2′) will return FALSE when viewing post about bananas. So if you want the same text to be applied to the category AND all its sub-categories, you’ll have to list them all. Syntax like in_category(2,11) is not allowed. You’ll have to use PHP || (logical OR) && (logical AND) in the expression.
< ?php if ( in_category(2) || in_category (11) || in_category (12)[more categories
abouth other fruits - this can get messy] ) {
echo 'This is about different kinds of fruits';
} else {
echo 'Not tasty! Not healthy!';
}
?>
in_categoryのループ外使用
通常、Loopが走る時だけそれが価値を割り当てられるWordPress PHP変数($post)に依存するので、このタグがLoopの中に使われなければなりません。しかし、あなたは手でこの変数を割り当てることができて、それからちょうどすばらしいタグを使用することができます。
⇒Normally, this tag must be used inside The Loop because it depends on a WordPress PHP variable ($post) that is assigned a value only when The Loop runs. However, you can manually assign this variable and then use the tag just fine.
たとえば、あなたが個々の地位がどんなカテゴリーでいるか、完全に異なるページを表示するあなたのThemeでsingle.php Template Fileを望むと仮定します。Calling in_category()、内部から、LoopはあなたのTemplateに便利でないかもしれません。それで、あなたのThemeのsingle.php.として以下を使ってください
⇒For example, suppose you want a single.php Template File in your Theme that will display a completely different page depending on what category the individual post is in. Calling in_category() from within The Loop may not be convenient for your Template. So use the following as your Theme’s single.php.
< ?php
$post = $wp_query->post;
if ( in_category('17') ) {
include(TEMPLATEPATH . '/single2.php');
} else {
include(TEMPLATEPATH . '/single1.php');
}
?>
⇒This will use single2.php as the Template if the post is in category 17 and single1.php otherwise.
category_id (integer) そうでなければ、ポストがカテゴリ17とsingle1.phpにあると、これはTemplateとしてsingle2.phpを使用するでしょう。
⇒(integer) The category ID of the category for which you wish to test. The parameter may either be passed as a bare integer or as a string:
- in_category(5)
- in_category(‘5′)
プラグインオプション
結局、だれかが自動的にこのすべてをする賢明なpluginを作るでしょう。 その時、この例は時代遅れになるでしょう。 しかしながら、CustomポストTemplates Pluginはただ一つのポストに関してテンプレートの創造を考慮します。 また、それは、どれがただ一つのポストだけではなく、与えられたカテゴリにおけるすべてのポストに使用されるかをどうテンプレートを加えるかに関する例に示します。 その例をデフォルトでpluginで論評されますが、適切な線について非論評することによって、容易に実行することができます。
⇒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.
関連テンプレートタグ
the_category, the_category_rss , single_cat_title, category_description, wp_dropdown_categories, wp_list_categories, in_category, get_category_parents, get_category_link, get_the_category
関連記事







