title: "模式" post_status: publish comment_status: open taxonomy: category: - gutenberg-docs post_tag: - Curating The Editor Experience - How To Guides - Repos
模式
区块模式是为用户提供独特且精心策划的编辑体验的最佳方式之一。
Prioritize starter patterns for any post type
When a user creates new content, regardless of post type, they are met with an empty canvas. However, that experience can be improved thanks to the option to have patterns from a specific type prioritized upon creation of a new piece of content. The modal appears each time the user creates a new item when there are patterns on their website that declare support for the core/post-content block types. By default, WordPress does not include any of these patterns, so the modal will not appear without at least two of these post content patterns being added.
To opt into this, include core/post-content in the Block Types for your pattern. From there, you can control which post types the pattern should show up for via the Post Types option. Here's an example of a pattern that would appear when creating a new post.
<?php
/**
* Title: New Event Announcement
* Slug: twentytwentytwo/new-event-announcement
* Block Types: core/post-content
* Post Types: post
* Categories: featured, text
*/
?>
<!-- wp:heading {"lock":{"move":false,"remove":true}} -->
<h2>Details</h2>
<!-- /wp:heading -->
<!-- wp:heading {"lock":{"move":false,"remove":true}} -->
<h2>Directions</h2>
<!-- /wp:heading -->
<!-- wp:heading {"lock":{"move":false,"remove":true}} -->
<h2>RSVP</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"lock":{"move":true,"remove":true}} -->
<p>To RSVP, please join the #fse-outreach-experiment in Make Slack. </p>
<!-- /wp:paragraph -->
<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"lock":{"move":true,"remove":false}} -->
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button">Learn more</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->
<!-- wp:cover {"useFeaturedImage":true,"dimRatio":80,"overlayColor":"primary","contentPosition":"center center","align":"full"} -->
<div class="wp-block-cover alignfull"><span aria-hidden="true" class="wp-block-cover__background has-primary-background-color has-background-dim-80 has-background-dim"></span><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write titleā¦","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size">We hope to see you there!</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->
Read more about this functionality in the Page creation patterns in WordPress 6.0 dev note and note that WordPress 6.1 brought this functionality to all post types.
优先展示模板创建起始模式
正如可以为新文章或页面优先展示模式,同样的体验也可应用于模板创建流程。当模式声明支持 templateTypes 属性时,创建匹配指定类型的模板时,这些模式将自动显示,同时提供从空白状态开始或使用当前模板回退选项。默认情况下,WordPress 不包含任何此类模式。
要启用此功能,模式需指定名为 templateTypes 的属性,这是一个包含可使用该模式作为完整内容的模板数组。以下示例展示了创建 404 模板时出现的模式:
register_block_pattern(
'wp-my-theme/404-template-pattern',
array(
'title' => __( '404 专用模板模式', 'wp-my-theme' ),
'templateTypes' => array( '404' ),
'content' => '<!-- wp:paragraph {"align":"center","fontSize":"x-large"} --><p class="has-text-align-center has-x-large-font-size">404 模式</p><!-- /wp:paragraph -->',
)
);
了解更多关于此功能的信息,请参阅 WordPress 6.3 开发说明中关于新建模板模态窗口的模式。
锁定模式
正如前文关于锁定 API 的章节所述,模式本身的各个方面可以被锁定,以保护设计的重要特性。这里有一个模式示例,其中不同的区块以不同方式被锁定。您可以在编辑器中构建这些模式,包括添加锁定选项,然后按照文档进行注册。
优先使用模式目录中的特定模式
自 WordPress 6.0 起,主题可通过 theme.json 注册来自模式目录的模式。为此,主题应使用 theme.json 中新增的顶级键 patterns。在此字段中,主题可列出要从模式目录注册的模式。patterns 字段是一个数组,包含模式目录中的模式别名。模式别名可从模式目录中单个模式视图页面的 URL 中提取。例如:URL https://wordpress.org/patterns/pattern/partner-logos 对应的别名是 partner-logos。
{
"patterns": [ "short-text-surrounded-by-round-images", "partner-logos" ]
}
内容创建者随后将在插入器的“模式”选项卡中,在匹配模式目录分类的类别下找到相应的模式。
其他资源
- 使用模板模式构建多种首页设计(WordPress 开发者博客)