作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个依赖于在注册自定义帖子类型时启用的自定义字段的 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/
我是一名优秀的程序员,十分优秀!