主题开发文档

分类、标签与自定义分类法

💡 云策文档标注

概述

本文档以食谱网站为例,解释了 WordPress 中分类、标签和分类法的概念及其区别。分类法是一种组织内容的方法,包括默认分类法(如分类和标签)和自定义分类法。

关键要点

  • 分类是层次化的分类法,用于组织内容(如食谱的早餐、午餐类别)。
  • 标签是非层次化的分类法,提供更具体的描述(如巧克力、姜等关键词)。
  • 分类法是 WordPress 中分类内容和数据的方法,包括默认和自定义类型。
  • 术语是分类法中的具体项目(如动物分类法中的狗、猫),可通过管理界面或 wp_insert_term() 函数创建。
  • 分类法和术语存储在 wp_terms、wp_term_taxonomy 和 wp_term_relationships 数据库表中。
  • WordPress 提供分类、标签和自定义分类法的模板层次结构,详细信息可参考 Taxonomy Templates 页面。
  • 自定义分类法允许创建新的分类方式(如烹饪时间或方法),建议将相关功能放入插件以确保内容持久性。

代码示例

wp_insert_term()

注意事项

  • 创建自定义分类法时,建议将其功能集成到插件中,以避免主题更换时内容丢失。
  • 更多关于创建自定义分类法的信息,请参考 Plugin Developer Handbook。

📄 原文内容

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.

Default Taxonomies

The default taxonomies in WordPress are:

  • categories: a hierarchical taxonomy that organizes content in the post Post Type
  • tags: a non-hierarchical taxonomy that organizes content in the post Post Type
  • post formats: a method for creating formats for your posts. You can learn more about these on the Post Formats page.

Terms

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.

Database Schema

Taxonomies and terms are stored in the following database tables:

  • wp_terms – stores all of the terms
  • wp_term_taxonomy – places the term in a taxonomy
  • wp_term_relationships – relates the taxonomy to an object (for example, category to post)
taxonomy-schema

Templates

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.

Custom Taxonomies

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.