主题开发文档

💡 云策文档标注

概述

Patterns 是 WordPress 主题开发中用于复用块组的强大工具,适用于多种场景,如提供可插入布局和运行 PHP 代码。本章介绍 Patterns 的基础概念、注册方法、PHP 使用、模板集成、块类型 Patterns、起始 Patterns 和块锁定功能。

关键要点

  • Patterns 允许主题开发者跨场景复用块组,提升开发效率。
  • 注册 Patterns 和模式类别是主题开发的基础步骤。
  • 在 Patterns 中使用 PHP 可实现国际化文本和媒体包含等高级功能。
  • 在模板中使用 Patterns 比直接使用模板更灵活,适用于多种情况。
  • 块类型 Patterns 为特定块提供定制化体验,优化用户编辑流程。
  • 起始 Patterns 可作为用户创建页面和模板的起点,简化构建过程。
  • 结合 Block Locking API 使用 Patterns 可以控制编辑体验,增强主题定制性。

📄 原文内容

Patterns are one of the most useful tools for theme development, allowing you to reuse groups of blocks across a wide variety of scenarios. Over time, you will likely use patterns more than any other theme feature available in WordPress.

In this chapter, you will learn what patterns are and dive into the various use cases where you will apply them in your themes. This will include everything from providing insertable layouts to your theme users to running PHP for loading assets and even more.

Navigating this chapter

Use the following links to locate a specific topic within this chapter. It’s best to read them in order, but if you are already familiar with the basic tenets of pattern development, feel free to jump to the topic that you’re looking for.

  • Introduction to Patterns: Provides an overview of what patterns are and how they work, giving you a foundation for later articles in the chapter.
  • Registering Patterns: A guide on registering/unregistering patterns and pattern categories for your theme.
  • Using PHP in Patterns: How and why to use PHP in your themes, including practical applications like internationalizing text and including media.
  • Usage in Templates: How to include patterns in templates and why you should use them over templates in many cases.
  • Block Type Patterns: A walkthrough of creating block-specific patterns that offer a more elegant experience for your theme users.
  • Starter Patterns: Building custom patterns as starting points for creating starting points for your theme users to build their pages and templates.
  • Patterns and Block Locking: How to use the Block Locking API to curate the editing experience for your theme users.