不能将保存的属性用于前端 Gutenberg 中的核心/模板部分块(WP 6.2)

问题描述 投票:0回答:0

我正在 Wordpress 中构建块主题。

我正在尝试使用已保存的自定义属性来使用

blocks.getSaveContent.extraProps
挂钩修改现有的核心块。具体来说,我正在尝试将此属性应用于
core/template-part
块。虽然这种方法适用于其他块,但我在尝试将其应用于
core/template-part
块的前端渲染时遇到了问题。

虽然我可以保存属性并使用它在编辑器中将类添加到块中,但我无法将它应用到前端。事实上,当尝试在站点编辑器中记录 blockTypes 时,

core/template-part
块不会记录为站点编辑器上呈现的块的一部分。

使用

blocks.getSaveElement
钩子,我可以获得
element
blockType
core/template-part
。但是,我无法将保存的值作为类添加到前端
core/template-part
块中,因为它没有随属性一起传递。

你对如何将这个属性作为一个类添加到前端的

core/template-part
块有什么想法吗?是否可以使用
getSaveElement
挂钩到现有的包装元素上?

这是脚本保存部分的代码:

/**
 * Save
 */
const saveTemplatePartAttributes = ( extraProps, blockType, attributes ) => {
    console.log(blockType.name)
    
    if ( enableSidebarControlsOnBlocks.includes( blockType.name ) ) {
        const { templatePartAttributes } = attributes;
        if ( templatePartAttributes ) {
            extraProps.className = classnames( extraProps.className, 'has-' + templatePartAttributes )
        }
    }    

    return extraProps;

};
wp.hooks.addFilter(
    'blocks.getSaveContent.extraProps',
    'btt-core/save-template-part-attributes',
    saveTemplatePartAttributes
);
wordpress wordpress-theming wordpress-gutenberg gutenberg-blocks wordpress-hook
© www.soinside.com 2019 - 2024. All rights reserved.