WordPress 中文文档
Template Tags/in category
出自WordPress Chinese 中文文档
目录 |
Description
Tests if the current post (or any specified post) is assigned to any of the specified categories.
ttin_category()/tt considers only the categories a post is directly assigned to (the checked categories in emWrite/Edit Post/em panel), not the parents of the assigned categories.
This tag can be used to test the current post within The Loop or (since Version 2.7) outside the Loop during a single post request. You can use it anywhere if you specify which post you want to test.
Usage
bool in_category( mixed var$category/var, mixed var$_post/var = null )
- tt$category/tt
- (mixed) (required) One or more categories specified by ID (integer), name or slug (string), or an array of these
- Default: Nonenoinclude
Notes
This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki已侦测回归模板: Template:Parameter/nowiki/pre The usage of this template is below: prenowiki已侦测回归模板: Template:Parameter/nowiki/pre Let's take a closer look at the parameters..
- name
- The name of the parameter.
- datatype
- The datatype that should be given for this parameter when called.
- description
- A short description of the parameter.
- importance
- Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
- default
- If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared./noinclude
- tt$_post/tt
- (mixed) (optional) The post (integer ID or object). Defaults to the current post in the Loop or the post in the main query.
- Default: Nonenoinclude
Notes
This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki已侦测回归模板: Template:Parameter/nowiki/pre The usage of this template is below: prenowiki已侦测回归模板: Template:Parameter/nowiki/pre Let's take a closer look at the parameters..
- name
- The name of the parameter.
- datatype
- The datatype that should be given for this parameter when called.
- description
- A short description of the parameter.
- importance
- Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
- default
- If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared./noinclude
History
- Since Version 2.5, you can specify categories by name.
- Since Version 2.7, you can specify categories by slug.
- Since Version 2.7, you can check against several categories.
- Since Version 2.7, you can use this function outside the WordPress Loop (during a single post query, presumably)
- Since Version 2.7, you can specify any post to test (not just the current one).
Examples
Testing the current post within the Loop
ttin_category()/tt is often used to take different actions within the Loop depending on the current post's category, e.g.
pre?php
if ( in_category( 'pachoderms' )) { // They have long trunks... } elseif ( in_category( array( 'Tropical Birds', 'small-mammals' ) )) { // They are warm-blooded... } else { // c. }
?/pre
Testing the current post outside the Loop
During a request for an individual post (usually handled by the ttsingle.php/tt template), you can test that post's categories even before the Loop is begun.
You could use this to switch templates like so:
pre?php
if ( in_category('fruit') ) { include 'single-fruit.php'; } elseif ( in_category('vegetables') ) { include 'single-vegetables.php'; } else { // Continue with normal Loop if ( have_posts() ) : while ( have_posts() ) : the_post(); // ... }
?/pre
(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.)
Testing if a post is in a descendant category
When showing a category archive, or showing a category's posts via ttquery_posts()/tt or ttget_posts()/tt, WordPress retrieves posts from the specified category emand/em any descendant (child) categories, but ttin_category()/tt tests only against a post's assigned categories, not ancestors (parents) of those categories.
For example, if you have a post assigned to the subcategory emFruit rarr; Bananas/em and not the category emFruit/em, the emFruit/em category archive will show the Bananas post, but calling codein_category('fruit')/code for that post always returns ttfalse/tt.
You can list both the ancestor category and every possible descendant category, e.g.
pre?php if ( in_category( array( 'fruits', 'apples', 'bananas', 'cantaloupes', 'guavas', /*etc*/ ) ) { // These are all fruits } ?/pre
or you can use the function below together with ttin_category()/tt:
pre?php in_category( 'fruit' ) || post_is_in_descendant_category( 11 ) ?/pre
pre?php /**
* Tests if any of a post's assigned categories are descendants of target categories * * @param mixed $cats The target categories. Integer ID or array of integer IDs * @param mixed $_post The post * @return bool * @see get_term_by() You can get a category by name or slug, then pass ID to this function * @uses get_term_children() Gets descendants of target category * @uses in_category() Tests against descendant categories * @version 2.7 */
function post_is_in_descendant_category( $cats, $_post = null ) { foreach ( (array) $cats as $cat ) { // get_term_children() accepts integer ID only $descendants = get_term_children( (int) $cat, 'category'); if ( $descendants in_category( $descendants, $_post ) ) return true; } return false; } ?/pre
Related
the_category, the_category_rss, single_cat_title, category_description, wp_dropdown_categories, wp_list_categories, in_category, get_category_parents, get_the_category get_category_link,
div style=background: #f7f7f7; border: 1px solid #000; padding: 10px;
How to pass parameters to tags with PHP function-style parameters

