in_category()判断文章是否属于某个分类目录

目录

描述

Tests if the current post (or any specified post) is assigned to any of the specified categories.

in_category() considers only the categories a post is directly assigned to (the checked categories in Write/Edit Post panel), not the parents of the assigned categories (but see Testing if a post is in a descendant category below).

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.

in_category() 描述

用法

<?php in_category$category$_post ?>

in_category() 用法

参数

$category

(mixed) (必填) One or more categories specified by ID (integer), name or slug (string), or an array of these

默认值: None

$_post

(mixed) (可选) The post (integer ID or object). Defaults to the current post in the Loop or the post in the main query.

默认值: None

in_category() 参数

返回值

(boolean) 

Whether the post is assigned to any of the specified categories.

in_category() 返回值

示例

Testing the current post within the Loop

in_category() is often used to take different actions within the Loop depending on the current post's category, e.g.

Testing the current post outside the Loop

During a request for an individual post (usually handled by the single.php template), you can test that post's categories even before the Loop is begun.

You could use this to switch templates like so:

(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 WP_Query() or get_posts(), or when hooking into the main query using is_main_query(), WordPress retrieves posts from the specified category and any descendant (child) categories, but in_category() 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 Fruit → Bananas and not the category Fruit, the Fruit category archive will show the "Bananas" post, but calling in_category('fruit') for that post always returns false.

You can list both the ancestor category and every possible descendant category, e.g.,

but you'd have to edit the code every time you moved or added any of the "Fruit" categories.

A more-flexible method is to use or adapt the post_is_in_descendant_category function defined below (you need to copy the function definition below into a template, plugin, or theme functions file before calling it). You can use it together with in_category() like this (in this example 11 is the "Fruit" category's ID number):

If you'd rather refer to the category by name you can use, e.g.,

post_is_in_descendant_category function

in_category() 示例

注意

  • 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).

in_category() 注意

历史

添加于 版本: 1.2.0

in_category() 历史

源文件

in_category() 函数的代码位于 wp-includes/category-template.php.

in_category() 源文件

相关