函數(shù)功能
注冊(cè)自定義Pattern 到WordPress古騰堡編輯器。
函數(shù)參數(shù)
| 參數(shù) | 數(shù)據(jù)類(lèi)型 | 是否必需 | 描述 | 默認(rèn)值 |
|---|---|---|---|---|
| $pattern_name | 字符串 | 是 | 包含命名空間的Pattern名稱(chēng) | 無(wú) |
| $pattern_properties | 數(shù)組 | 是 | 包含Pattern屬性的數(shù)組。 | 無(wú) |
$pattern_properties 數(shù)組可以包含以下幾項(xiàng)數(shù)據(jù)。
| 參數(shù) | 數(shù)據(jù)類(lèi)型 | 是否必需 | 描述 |
|---|---|---|---|
| title | 字符串 | 是 | 模式標(biāo)題 |
| content | 字符串 | 是 | 古騰堡編輯器的內(nèi)容代碼 |
| description | 字符串 | 否 | Pattern作用的詳細(xì)說(shuō)明 |
| categories | 字符串 | 否 | Pattern分類(lèi) |
| keywords | 字符串 | 否 | Pattern關(guān)鍵詞,用于搜索 |
| viewportWidth | 字符串 | 否 | 在插入時(shí)指定Pattern的寬度 |
函數(shù)返回值
如果自定義Pattern注冊(cè)成功,返回true,否則為false。
使用示例
下面的示例注冊(cè)了一個(gè)包含兩個(gè)按鈕的自定義Pattern。數(shù)組中 content 的內(nèi)容可以在古騰堡編輯器中編輯好后,切換到古騰堡編輯器的代碼模式,直接復(fù)制代碼過(guò)來(lái)。
register_block_pattern(
'wpdocs-my-plugin/my-awesome-pattern',
array(
'title' => __( 'Two buttons', 'wpdocs-my-plugin' ),
'description' => _x( 'Two horizontal buttons, the left button is filled in, and the right button is outlined.', 'Block pattern description', 'wpdocs-my-plugin' ),
'content' => "<!-- wp:buttons {\"align\":\"center\"} -->\n<div class=\"wp-block-buttons aligncenter\"><!-- wp:button {\"backgroundColor\":\"very-dark-gray\",\"borderRadius\":0} -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-background has-very-dark-gray-background-color no-border-radius\">" . esc_html__( 'Button One', 'wpdocs-my-plugin' ) . "</a></div>\n<!-- /wp:button -->\n\n<!-- wp:button {\"textColor\":\"very-dark-gray\",\"borderRadius\":0,\"className\":\"is-style-outline\"} -->\n<div class=\"wp-block-button is-style-outline\"><a class=\"wp-block-button__link has-text-color has-very-dark-gray-color no-border-radius\">" . esc_html__( 'Button Two', 'wpdocs-my-plugin' ) . "</a></div>\n<!-- /wp:button --></div>\n<!-- /wp:buttons -->",
)
);