register_block_pattern 注册自定义Pattern

发布于:
更新时间:2021-08-20

函数功能

注册自定义Pattern 到WordPress古腾堡编辑器。

函数参数

参数数据类型是否必需描述默认值
$pattern_name字符串包含命名空间的Pattern名称
$pattern_properties数组包含Pattern属性的数组。

$pattern_properties 数组可以包含以下几项数据。

参数数据类型是否必需描述
title字符串模式标题
content字符串古腾堡编辑器的内容代码
description字符串Pattern作用的详细说明
categories字符串Pattern分类
keywords字符串Pattern关键词,用于搜索
viewportWidth字符串在插入时指定Pattern的宽度

函数返回值

如果自定义Pattern注册成功,返回true,否则为false。

使用示例

下面的示例注册了一个包含两个按钮的自定义Pattern。数组中 content 的内容可以在古腾堡编辑器中编辑好后,切换到古腾堡编辑器的代码模式,直接复制代码过来。

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 -->",
    )
);

我们提供 WordPress主题和插件定制开发服务

本站长期承接 WordPress主题、插件、基于 WooCommerce 的商店商城开发业务。 我们有 10 年WordPress开发经验,如果你想 用WordPress开发网站, 请联系微信: iwillhappy1314,或邮箱: amos@wpcio.com 咨询。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

*