【问题标题】:Add custom block in a Wordpress block theme(Not with the plugin)在 Wordpress 块主题中添加自定义块(不使用插件)
【发布时间】:2022-08-16 15:21:43
【问题描述】:

基本上我想开发一个wordpress块主题。为了便于自定义,我想在我的主题中添加一些自定义块。但我不想为此创建一个插件。自定义块将在我的主题中。当有人安装时我的主题他将获得那些内置在我的主题中的自定义块。我怎样才能实现它,我怎样才能组织我的文件夹结构?

  • 没有新的更新?

标签: wordpress wordpress-theming gutenberg-blocks


【解决方案1】:

您需要做的就是创建一个源代码文件夹内的主题,然后将块文件夹放在那里(如下所示的结构)。

文件夹结构可能是:

-- 主题文件夹 --> 源 --> 块文件夹 > 块文件(edit.js、index.js、save.js 等)

对于块文件:

现在是时候安装 wp-scripts 包来使用 wp 块了。为此,请更新您的包.json文件添加以下行。然后在终端中转到主题文件夹并命令'npm 安装' 它会安装你所有的依赖包然后给出 'npm 开始' 命令。

{
"main": "build/index.js",
"scripts": {
    "build": "wp-scripts build",
    "format": "wp-scripts format",
    "lint:css": "wp-scripts lint-style",
    "lint:js": "wp-scripts lint-js",
    "packages-update": "wp-scripts packages-update",
    "plugin-zip": "wp-scripts plugin-zip",
    "start": "wp-scripts start"
},
"devDependencies": {
    "@wordpress/scripts": "^23.4.0"
}
}

注意:PHP方式需要通过register_block_type函数注册块,JS方式需要registerBlockType注册块。否则,上面的代码将不起作用。以上代码将仅创建您的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    相关资源
    最近更新 更多