php - 如何在古腾堡编辑器 wordpress 中使用 get_theme_mod?
问题描述
在我的旧 WordPress 主题中(在古腾堡之前),我曾经get_theme_mod
为主题中的某些内容获取自定义值。
get_theme_mod( 'news_custom_headline' );
现在我想使用古腾堡编辑器,但仍想从定制器访问数据。我该怎么做这样的事情:
save({ attributes }) {
return <p>Value from backend: get_theme_mod( 'news_custom_headline' ) </p>;
}
解决方案
你不这样做。我让它与服务器端渲染块一起工作:
在我的index.js
:
registerBlockType('test/custom-php-render-block', {
title: "Custom Block Server Side Rendering",
category: 'widgets',
edit: () => { return <p>This is just a placeholder!</p>},
save: () => { return null }
});
在functions.php
:
function register_my_custom_block() {
wp_register_script( 'custom-block-php-script', get_template_directory_uri() . '/build/index.js', array('wp-blocks', 'wp-editor'));
wp_enqueue_script('custom-block-php-script');
register_block_type('test/custom-php-render-block', [
'editor_script' => 'custom-block-php-script',
'render_callback' => 'custom_block_php-render'
]);
}
add_action( 'init', 'register_my_custom_block');
function custom_block_php-render($attr, $content) {
return '<p>From customizer: ' . get_theme_mod('your-setting') . '</p>';
}
您可以在此处阅读更多相关信息:https ://awhitepixel.com/blog/wordpress-gutenberg-create-custom-block-part-9-dynamic-blocks-php-render/
推荐阅读
- javascript - 无法将 Laravel 刀片文件中的道具传递给 Vue 组件
- mysql - SQL 查询从 2 个公共 ID 表中获取每行的未结余额
- c# - 在 netCore 中管理 SDK 许可证密钥
- c# - Itext c# - 将 html 转换为 pdf/a 时出错 [xobjects 中不允许使用 smask 键]
- authentication - 缺少 ADFS EnableIdpInitiatedSignonpage 属性
- javascript - 在 JS 类中调用 Setter 的问题
- c++ - 删除重复链表
- raspberry-pi - wxPython 窗口在 RPi 上随机冻结,然后行为怪异
- r - 如何在 R 中使用“内部”时仅过滤特定信息
- postman - 在 Postman 中为导入的集合设置授权