跳转到正文

样式

样式是基于文件的模板,注册为 WordPress 区块样式。它们会显示在区块插入器中,用户可以将其放置在任意位置。

与页面不同,样式会在内存中注册,不会创建数据库条目。

pattern.json

每个样式文件夹都需要一个 pattern.json 配置文件:

JSON
{
  "name": "hero-section",
  "title": "Hero Section",
  "description": "A hero section with heading and CTA",
  "categories": ["featured", "header"],
  "keywords": ["hero", "banner", "cta"]
}

属性

属性 类型 默认值 描述
name string 必填 唯一标识符(带 blockstudio/ 前缀)
title string 必填 区块插入器中显示的标题
description string "" 插入器中显示的描述
categories string[] [] 样式分类(e.g.、featured、header、footer)
keywords string[] [] 插入器的搜索关键词
viewportWidth number – 预览宽度(像素)
blockTypes string[] [] 关联的区块类型
postTypes string[] [] 限定为特定文章类型
inserter boolean true 是否在插入器中显示

示例

pattern.json:

JSON
{
  "name": "hero-with-cta",
  "title": "Hero with CTA",
  "description": "A full-width hero section with heading and call-to-action buttons",
  "categories": ["featured", "header"],
  "keywords": ["hero", "banner", "cta", "landing"],
  "viewportWidth": 1200
}

index.php:

HTML
<block name="core/cover" url="https://example.com/hero-bg.jpg" dimRatio="60">
  <h1>Build Something Amazing</h1>
  <p>Create beautiful websites with the power of blocks.</p>
  <block name="core/buttons">
    <block name="core/button" url="/get-started">Get Started</block>
    <block name="core/button" url="/learn-more" className="is-style-outline">Learn More</block>
  </block>
</block>

自定义路径

默认情况下,Blockstudio 会扫描 get_template_directory() . '/patterns'。使用此筛选器添加其他路径:

PHP
add_filter( 'blockstudio/patterns/paths', function( $paths ) {
    $paths[] = get_stylesheet_directory() . '/custom-patterns';
    $paths[] = MY_PLUGIN_PATH . '/patterns';
    return $paths;
} );

负责组合多个根目录文件的运行时集成可以使用逻辑发现源替代文件系统扫描。样式清单和源模板会根据最终可见的逻辑清单解析。

PHP API

PHP
// Get all registered patterns
$patterns = Blockstudio\Patterns::patterns();

// Get a specific pattern
$pattern = Blockstudio\Patterns::get_pattern('hero-section');

// Get registered paths
$paths = Blockstudio\Patterns::get_registered_paths();

与页面的比较

方面 页面 样式
存储 作为文章同步到数据库 在内存中注册
配置 page.json pattern.json
同步 显式执行,使用 wp bs pages sync 无需同步
模板锁定 是 否
用途 预定义页面内容 可重复使用的区块模板