主题开发文档

💡 云策文档标注

概述

样式变体是主题中 theme.json 文件的替代版本,作为自定义 JSON 文件存储在主题的 /styles 文件夹中,允许用户选择不同设计风格应用于网站。

关键要点

  • 样式变体是 theme.json 的变体文件,可覆盖主题的默认设置和样式,提供如“皮肤”般的多样化设计选项。
  • 变体文件需放置在主题的 /styles 文件夹中,并可通过 title 属性配置在用户界面中显示的标题。
  • 用户选择样式变体后,其 JSON 数据会迁移到数据库并作为用户自定义存储,覆盖主题的 theme.json 设置。
  • 样式变体与子主题的主要区别在于:变体仅通过单个 JSON 文件覆盖 theme.json,而子主题可覆盖父主题的任何内容;变体数据保存为用户自定义,更新时不会自动应用到已保存的用户选择。

代码示例

{
	"version": 2,
	"title": "Latte",
	"settings": {},
	"styles": {}
}

注意事项

  • 样式变体适用于提供主题的多样化设计选项,但更新变体文件时,已保存的用户自定义不会自动更新,用户需手动切换回变体以获取更新。
  • 在需要更广泛覆盖(如模板文件或函数)时,子主题可能比样式变体更合适。

📄 原文内容

Unlike most things here in the Global Settings and Styles documentation, style variations are not things you define within theme.json. Instead, they are “variations” to your existing theme.json file that you can offer to users.

A more accurate name for this feature might be Global Settings and Styles Variations. Or simply theme.json variations.

What are style variations?

Style variations are essentially alternative versions of theme.json that you can ship with your theme. They are custom-named JSON files that are stored in your theme’s /styles folder. Any setting or style that you can add to theme.json can also be added to your style variation JSON file.

This lets your users pick and choose which variation they want to use on their site. In a way, they are “skins” for your theme.

For example, suppose you’ve created a restaurant theme and have kept the colors and typography pretty basic so that it covers a lot of different restaurant site designs. Further suppose that you wanted to offer more variety, variations on that initial design. You could create a style variation that caters more toward seafood restaurants with fun fonts and an ocean-oriented color palette. Or maybe you want to set the mood for coffee shops that might be running your theme.

That’s where style variations can really shine. You can bundle each of these alternative designs for your theme and let your users decide which is the best option for their site.

Here is a look at the style variations that are bundled with the default Twenty Twenty-Three theme:

WordPress Site Editor > Styles sub-screen, which is showing a grid of style variations with a red one in the preview panel.

When a user selects a style variation, the JSON data is migrated to the site’s database and stored as a user customization. This allows the data to overrule the theme’s primary theme.json settings and styles.

Adding custom style variations

The style variations feature is relatively straightforward if you already understand how theme.json works, but there are a couple of differences.

The first difference between theme.json and style variations are their names and placement in your theme’s folder structure. theme.json lives in the root of your theme folder and is considered the default variation. But custom variations must have a unique filename and be placed in the /styles folder.

Let’s assume you’ve built that restaurant theme mentioned earlier in this document. Now you want to add a couple of variations named Swashbuckler (for that seafood design) and Latte (for the coffee shop design). This is how your theme files would be organized:

/your-theme-folder
	/styles
		/latte.json
		/swashbuckler.json
	/theme.json

Style variations are simply variations of theme.json, so you have full access to everything in the theme.json specification at your fingertips. 

The second difference between theme.json and style variations is the variation title. You can configure this by adding the title property to your custom JSON files.

Building off the Latte variation example above, you would open your /styles/latte.json file and add it, as shown in this code snippet:

{
	"version": 2,
	"title": "Latte",
	"settings": {},
	"styles": {}
}

The title field is used to represent your variation in the user interface. It is not a required field (WordPress will fall back to your variation), but it does make for a nicer user experience.

Style variations vs. child themes

If you are familiar with the concept of child themes, which are covered in the Advanced Topics documentation, you may be wondering what the differences between them and style variations are.

The most obvious difference is that a style variation is limited to a single JSON file that overrides the primary theme.json, whereas a child theme can override anything from its parent theme. So it’s probably better to look at the one area they are similar: the JSON file itself.

In a child theme, the theme.json simply overrides its parent’s theme.json file. In a style variation—and this is where the major difference occurs—the variation’s JSON file overrides the theme.json file and its data is saved to the database.

Once a user selects a style variation of a theme, everything in the variation’s JSON file is treated as a user customization. Essentially, WordPress stores that initial data in the same way as if the user had simply designed the colors, typography, spacing, etc. from the interface. This is an important distinction to make because it means that when you update a style variation in a future theme release, the user will not receive those changes if they have already saved the style variation.

It is possible for users to switch to a variation and switch back to the one they were using to get the update.

Style variations can be a great feature to add to your theme, but they have a specific use case. Sometimes child themes make more sense.