本文档以食谱网站为例,解释了 WordPress 中分类、标签和分类法的概念及其区别。分类法是一种组织内容的方法,包括默认分类法(如分类和标签)和自定义分类法。
wp_insert_term()Categories, tags, and taxonomies are all related and can be easily confused.
We’ll use the example of building a theme for a recipe website to help break down categories, tags, and taxonomies.
In our recipe website, the categories would be Breakfast, Lunch, Dinner, Appetizers, Soups, Salads, Sides, and Desserts. All recipes will fit within those categories, but users might want to search for something specific like chocolate desserts or ginger chicken dinners.
Chocolate, ginger, and chicken are all examples of tags. They are another level of specificity that provides meaning to the user.
Lastly, there are taxonomies. In reality, categories and tags are examples of default taxonomies which simply are a way to organize content. Taxonomies are the method of classifying content and data in WordPress. When you use a taxonomy you’re grouping similar things together. The taxonomy refers to the sum of those groups. As with Post Types, there are a number of default taxonomies, and you can also create your own.
Recipes are normally organized by category and tag, but there are some other helpful ways to break the recipes down to be more user friendly. For example, the recipe website might want an easy way to display recipes by cook time. A custom taxonomy of cook time with 0-30 min, 30-min to an hour, 1 to 2 hours, 2+ hours would be a great breakdown. Additionally, cook method such as grill, oven, stove, refrigerator, etc would be another example of a custom taxonomy that would be relevant for the site. There could also be a custom taxonomy for how spicy the recipe is and then a rating from 1-5 on spiciness.
The default taxonomies in WordPress are:
Terms are items within your taxonomy. So, for example, if you have the Animal taxonomy you would have the terms, dogs, cats, and sheep. Terms can be created via the WordPress admin, or you can use the wp_insert_term() function.
Taxonomies and terms are stored in the following database tables:

WordPress offers several different hierarchies of templates for categories, tags, or custom taxonomies. More details on their structure and usage may be found on the Taxonomy Templates page.
It is possible to create new taxonomies in WordPress. You may, for example, want to create an author taxonomy on a book review website, or an actor taxonomy on a film site. As with custom post type it is recommended that you put this functionality in a plugin. This ensures that when the user changes their website’s design, their content is preserved in the plugin.
You can read more about creating custom taxonomies in the Plugin Developer Handbook.