WordPress determines whether an article belongs to a category tag: in_ category

WordPress template tags in_ Category is used to determine whether an article belongs to a certain category. This function can only be used in the main loop of the loop. If the article ID is explicitly specified, it can be used anywhere.

in_category( int|string|array $category, int|object $post = null )

Function parameters


Integer / string / array, null by default

The ID of the classification can be multiple.


Integer / object, default value: current article ID

If it is used in the main loop, you can not specify the value of this parameter. By default, it takes the ID of the current article. If it is used outside the main loop, you need to specify the article ID explicitly

Examples of function usage

	if( in_category( array(2,6,8), $post->ID )) {
		echo 'yes';
	} else {
		echo 'no';

Extended reading

in_ The category() function is located in: WP includes / category- template.php

Related tags:

  • the_ category()
  • the_ category_ rss()
  • single_ cat_ title()
  • category_ description()
  • wp_ dropdown_ categories()
  • wp_ list_ categories()
  • get_ category_ parents()
  • get_ the_ category()
  • get_ category_ link()
Related Article
WordPress comment date tag: comment_date

WordPress template tags comment_ Date is used to output the date of a comment, usually in a custom comment template. Read more

WordPress comment summary tag: comment_excerpt

WordPress template tags comment_ Excerpt is used to output the summary of comments. The default output is 20 characters. It Read more

WordPress comment time tag: comment_time

WordPress template tags comment_ Time is used to output the publishing time of the current comment, which is the same Read more

WordPress RSS format comment author name tag: comment_author_rss

WordPress template tags comment_ author_ RSS is used to output the comment author name in RSS format, which has no Read more

Leave a Reply

Your email address will not be published. Required fields are marked *

To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image