繁体   English   中英

从 wordpress 古腾堡侧边栏插件更改帖子的标签/类别

[英]Changing a post's tag / category from a wordpress gutenberg sidebar plugin

我正在尝试从 WordPress Gutenberg 侧边栏插件(使用 React/JavaScript)添加/删除帖子的标签/类别。 关于此用例实施的信息似乎很少,我正在寻求社区对您可能遇到的可行方法的意见。

当前实施

我有一个侧边栏插件,有几个面板。 其中一个负责从帖子中添加/删除类别/标签。 组件使用以下方式呈现:

MyComponent = props => {
    return (
             <PanelBody title="My Title">
                <PanelRow>
                    <TabPanel
                        className="tab-panel"
                        activeClass="active-tab"
                        onSelect={(tabName) => props.onItemChange(tabName)}
                        tabs={_data}
                    >
                        {tab => (
                            <div className="tab-content">
                                <div
                                    className="description"
                                    dangerouslySetInnerHTML={{ __html: tab.description }}
                                ></div>
                                <div className="actions">
                                    <Button isSecondary onClick={() => props.onTaxonomiesAdd(props.category, props.tag)}>Add Tag / Category!</Button>
                                </div>
                            </div>
                        )}
                    </TabPanel>
                </PanelRow>
           </PanelBody>
     );
};

 

单击按钮时,我想为帖子添加指定的标签/类别。 点击事件在 WithDispatch 高阶组件中被成功检测并触发,如下所示:

export default compose([
    withSelect(select => {  // WithSelect Routines Here  }),
    withDispatch(dispatch => {
        return {
            onTaxonomiesAdd: (category, tag) => {
                 //Add Taxonomy Items here
                 alert("I'm firing successfully");
            }
        }

到目前为止,我偶然发现的最接近的方法是:

wp.data.dispatch( 'core' ).editEntityRecord( 'postType', 'contributor', currentPost.id, { 'topic': [ term_id ] } );

...但我还没有让类似的东西正常工作。

你们中有人找到了实现这一结果的解决方案吗?

继上面的链接之后,我使用以下方法成功实现了用例(但肯定需要一些改进):

export default compose([
    withSelect(select => {  
  
            tags: select( 'core/editor' ).getEditedPostAttribute( 'tags' ),
            tag_panel_open: select( 'core/edit-post' ).isEditorPanelOpened( 'taxonomy-panel-tags' ),
            categories: select( 'core/editor' ).getEditedPostAttribute( 'categories' ),
            category_panel_open: select( 'core/edit-post' ).isEditorPanelOpened( 'taxonomy-panel-category' ),        
    }),
    withDispatch(dispatch => {
        return {
            onTaxonomiesAdd: (tags, tag_panel_open, categories, category_panel_open, category, tag) => {

                //Add tag
                tags.push(tag);  // Where tag is the tag id to add
                dispatch( 'core/editor' ).editPost( { 'tags': tags } );

                // close and re-open the panel to reload the term data
                if ( tag_panel_open ) {
                    dispatch( 'core/edit-post' ).toggleEditorPanelOpened( 'taxonomy-panel-tags' );
                    dispatch( 'core/edit-post' ).toggleEditorPanelOpened( 'taxonomy-panel-tags' );
                }                   

                //Add category
                categories.push(category);  // Where categoryis the tag category to add
                dispatch( 'core/editor' ).editPost( { 'categories': categories } );

                // close and re-open the panel to reload the term data
                if ( category_panel_open ) {
                    dispatch( 'core/edit-post' ).toggleEditorPanelOpened( 'taxonomy-panel-category' );
                    dispatch( 'core/edit-post' ).toggleEditorPanelOpened( 'taxonomy-panel-category' );
                } 
            }
        }

我不是 100% 认为关于 WithSelect 的方法以及随后需要将这些变量传递给 WithDispatch 函数的方法无论如何都是理想的。 那是我的下一场战斗(!),但我想我会与其他希望实现此用例的人分享这个。

同样的问题或问题。 我创建了自定义帖子类型并为标签注册了分类法。 当我填充标签元框并在 Gutenberg 中保存/更新帖子时,Wordpress 没有保存它。 重新加载后,页面标签元框被清除,标签不会被保存。 但是如果关闭古腾堡并执行相同的操作,它就可以工作! Wordpress 正在保存和显示标签元框中的标签。 什么问题? 阿努的建议?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM