主题开发文档

💡 云策文档标注

概述

WordPress 中的内容类型统称为 Post Types,包括默认类型和自定义类型,用于分类和显示内容。所有 Post Types 都存储在 wp_posts 数据库表中,通过 post_type 列区分。

关键要点

  • Post Types 是 WordPress 中所有内容类型的统称,如文章、页面等,用于组织内容。
  • 默认 Post Types 包括 Post、Page、Attachment、Revision、Navigation menu、Block templates 和 Template parts,可通过插件或主题修改但不建议移除。
  • Post 类型用于博客,按时间倒序显示,支持分类和标签,相关模板文件包括 single、archive、category 等。
  • Page 类型是静态内容,无时间戳,支持层级结构,相关模板文件包括 page、front-page 等。
  • Attachment 类型用于媒体文件,存储元数据,相关模板文件包括 MIME_type、attachment 等。
  • Custom Post Types 允许创建自定义内容类型,建议在插件中实现以确保内容可移植性,相关模板文件包括 single-{post-type}、archive-{post-type} 等。

📄 原文内容

There are many different types of content in WordPress. These content types are normally described as Post Types, which may be a little confusing since it refers to all different types of content in WordPress. For example, a post is a specific Post Type, and so is a page.

Internally, all of the Post Types are stored in the same place — in the wp_posts database table — but are differentiated by a database column called post_type.

In addition to the default Post Types, you can also create Custom Post Types.

The Template files page briefly mentioned that different Post Types are displayed by different Template files.  As the whole purpose of a Template file is to display content a certain way, the Post Types purpose is to categorize what type of content you are dealing with. Generally speaking, certain Post Types are tied to certain template files.

Default Post Types

There are several default Post Types readily available to users or internally used by the WordPress installation. The most common are:

  • Post (Post Type: ‘post’)
  • Page (Post Type: ‘page’)
  • Attachment (Post Type: ‘attachment’)
  • Revision (Post Type: ‘revision’)
  • Navigation menu (Post Type: ‘nav_menu_item’)
  • Block templates (Post Type: ‘wp_template’)
  • Template parts (Post Type: ‘wp_template_part’)

The Post Types above can be modified and removed by a plugin or theme, but it’s not recommended that you remove built-in functionality for a widely-distributed theme or plugin.

It’s out of the scope of this handbook to explain other post types in detail. However, it is important to note that you will interact with and build the functionality of navigation menus and that will be detailed later in this handbook.

Post

Posts are used in blogs. They are:

  • displayed in reverse sequential order by time, with the newest post first
  • have a date and time stamp
  • may have the default taxonomies of categories and tags applied
  • are used for creating feeds

The template files that display the Post post type are:

  • single and single-post
  • category and all its iterations
  • tag and all its iterations
  • taxonomy and all its iterations
  • archive and all its iterations
  • author and all its iterations
  • date and all its iterations
  • search
  • home
  • index

Read more about Post Template Files in classic themes.

Page

Pages are a static Post Type, outside of the normal blog stream/feed. Their features are:

  • non-time dependent and without a time stamp
  • are not organized using the categories and/or tags taxonomies
  • can be organized in a hierarchical structure — i.e. pages can be parents/children of other pages

The template files that display the Page post type are:

  • page and all its iterations
  • front-page
  • search
  • index

Read more about Page Template Files in classic themes.

Attachment

Attachments are commonly used to display images or media in content, and may also be used to link to relevant files. Their features are:

  • contain information (such as name or description) about files uploaded through the media upload system
  • for images, this includes metadata information stored in the wp_postmeta table (including size, thumbnails, location, etc)

The template files that display the Attachment post type are:

  • MIME_type
  • attachment
  • single-attachment
  • single
  • index

Read more about Attachment Template Files in classic themes.

Custom Post Types

Using Custom Post Types, you can create your own post type. It is not recommend that you place this functionality in your theme. This type of functionality should be placed/created in a plugin. This ensures the portability of your user’s content, and that if the theme is changed the content stored in the Custom Post Types won’t disappear.

You can learn more about creating custom post types in the WordPress Plugin Developer Handbook.

While you generally won’t develop Custom Post Types in your theme, you may want to code ways to display Custom Post Types that were created by a plugin.  The following templates can display Custom post types:

  • single-{post-type}
  • archive-{post-type}
  • search
  • index

Read more about Custom Post Type Templates in classic themes.