首页 > 解决方案 > 如何在古腾堡编辑器 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>;
}

标签: phpwordpresswebwordpress-themingwordpress-gutenberg

解决方案


你不这样做。我让它与服务器端渲染块一起工作:

在我的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/


推荐阅读