gpt4 book ai didi

wordpress - wp.​​data.select ('core' ).getPostType ('post-type' ) 在古腾堡中返回未定义

转载 作者:行者123 更新时间:2023-12-04 12:41:51 25 4
gpt4 key购买 nike

我正在构建一个依赖于在注册自定义帖子类型时启用的自定义字段的 Wordpress 插件。我想检查帖子类型对象中 supports 中的 custom-fields 键是否存在(并且是 true)。但是,当我调用 wp.data.select('core').getPostType('post-type') 时,它返回 undefined。如果我直接从控制台调用它,我会得到我期望的帖子类型对象,所以我不确定为什么它在我的代码中不起作用。

我试过从几个地方调用它。

例如注册插件面板时:

// Loading `wp.editPosts`, `wp.plugins`, etc. excluded for brevity.

const Component = () => {
const postType = wp.data.select('core/editor').getCurrentPostType();
const postTypeObj = wp.data.select('core').getPostType(postType);

if (postTypeObj.supports.hasOwnProperty('custom-fields')) {
return (
<PluginDocumentSettingPanel
name="my-plugin-name"
title={ __('My Plugin Title', 'pb') }
>
<MyPlugin/>
</PluginDocumentSettingPanel>
);
}

return;
};

registerPlugin('my-plugin-name', {
render: Component,
icon: '',
});

或通过 withSelect 在插件中:

// Loading `wp.compose`, `wp.data`, etc. excluded for brevity.

class MyPlugin extends React.Component {
constructor(props) {
super(props);

this.state = {
// I'll do something with this later on
postTypeObj: props.postTypeObj,
};
}

render() {
return (
<div></div>
);
}
}

export default compose([
withSelect(select => {
const {
getCurrentPostType,
} = select('core/editor');

const {
getPostType,
} = select('core');

return {
postTypeObj: getPostType(getCurrentPostType()),
}
}),
])(MyPlugin);

无论我在哪里调用它,post 类型对象都会返回 undefined

我使用的是 Gutenberg 插件,版本 6.5.0 和 WordPress 版本 5.2.3

感谢任何帮助。

最佳答案

调用 getCurrentPostType() 将返回例如发布。您可能也不需要将其包装在 getPostType 中。此外,根据 https://github.com/WordPress/gutenberg/issues/13555 ,您将需要“订阅”(wp.data.subscribe),因为这是一个异步调用,因此在您第一次运行它时将返回 null。

关于wordpress - wp.​​data.select ('core' ).getPostType ('post-type' ) 在古腾堡中返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58137429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com