首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何向现有的gutenberg文档侧边栏添加新面板?

如何向现有的gutenberg文档侧边栏添加新面板?
EN

Stack Overflow用户
提问于 2019-11-17 18:23:58
回答 1查看 1.5K关注 0票数 0

我尝试向现有的gutenberg文档侧边栏添加两个新面板。一个应该包含一个单选按钮菜单来设置标题图像的高度,另一个应该包含一个文本字段来输入页面的字幕。

但是,由于我不想使用过时的元盒技术,所以几乎没有任何教程来实现这一点。我只找到了下面的一段代码,但是我不知道如何根据我的需要和把它放在哪里;) --我的编码知识还不够好,但是我仍然需要在我的主题中实现这个特性。

代码语言:javascript
复制
const { registerPlugin } = wp.plugins
const { PluginDocumentSettingPanel } = wp.editPost

const PluginDocumentSettingPanelDemo = () => (
    <PluginDocumentSettingPanel
        name="custom-panel"
        title="Custom Panel"
        className="custom-panel"
    >
        Custom Panel Contents
    </PluginDocumentSettingPanel>
)
registerPlugin('plugin-document-setting-panel-demo', {
    render: PluginDocumentSettingPanelDemo
})

你能猜到如何实现我的想法吗?感谢您的支持和奥地利的问候!塞缪尔

EN

回答 1

Stack Overflow用户

发布于 2019-11-19 11:34:04

首先,注册元字段,这样就有了保存值的位置。这会出现在插件文件或functions.php中。

代码语言:javascript
复制
register_post_meta('post', 'customname_meta_subtitle', array(
    'show_in_rest' => true,
    'type' => 'string',
    'single' => true
));

register_post_meta('post', 'customname_meta_header_height', array(
    'show_in_rest' => true,
    'type' => 'string',
    'single' => true
));

你可以检查一下文档。我们告诉WordPress创建2个新的post元字段,其中键为customname_meta_subtitlecustomname_meta_header_height,我们将在Gutenberg部分中使用这些字段。

对于ES代码,您需要以下内容:

代码语言:javascript
复制
const { registerPlugin } = wp.plugins
const { PluginDocumentSettingPanel } = wp.editPost
const { RadioControl, TextControl } = wp.components
const { withState } = wp.compose
const { withSelect, withDispatch } = wp.data

let SubtitleControl = ({ subtitle, handleSubtitleChange }) => (
    <TextControl
        label="Set subtitle"
        value={subtitle}
        onChange={subtitle => handleSubtitleChange(subtitle)}
    />
);

SubtitleControl = withSelect(
    (select) => {
        return {
            subtitle: select('core/editor').getEditedPostAttribute('meta')['customname_meta_subtitle']
        }
    }
)(SubtitleControl);

SubtitleControl = withDispatch(
    (dispatch) => {
        return {
            handleSubtitleChange: (value) => {
                dispatch('core/editor').editPost({ meta: { customname_meta_subtitle: value } })
            }
        }
    }
)(SubtitleControl);

let HeaderImageHeightControl = ({ height, handleHeightChange }) => (
    <RadioControl
        label="Set image height"
        help="Set the height of the header image"
        selected={height}
        options={[
            { label: '100', value: '1' },
            { label: '200', value: '2' },
        ]}
        onChange={handleHeightChange}
    />
);

HeaderImageHeightControl = withSelect(
    (select) => {
        return {
            height: select('core/editor').getEditedPostAttribute('meta')['customname_meta_header_height']
        }
    }
)(HeaderImageHeightControl);

HeaderImageHeightControl = withDispatch(
    (dispatch) => {
        return {
            handleHeightChange: value => {
                dispatch('core/editor').editPost({ meta: { customname_meta_header_height: value } })
            }
        }
    }
)(HeaderImageHeightControl);

const PluginDocumentSettingPanelDemo = () => (
    <PluginDocumentSettingPanel
        name="custom-panel"
        title="Custom Panel"
        className="custom-panel"
    >
        <SubtitleControl />
        <HeaderImageHeightControl />
    </PluginDocumentSettingPanel>
)
registerPlugin('plugin-document-setting-panel-demo', {
    render: PluginDocumentSettingPanelDemo
})

大部分代码都是在正式WP教程中描述的,但是可以随意询问是否有什么不清楚的地方。

最后,要使用新值,可以这样做:

代码语言:javascript
复制
<h1><?php echo get_post_meta(get_the_ID(), 'customname_meta_subtitle')[0]; ?></h1>
<h1><?php echo get_post_meta(get_the_ID(), 'customname_meta_header_height')[0]; ?></h1>

这将在post模板文件中使用,用于元字段信息的前端可视化。

我希望这能帮到你!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58903733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档