问题描述
我正在尝试在我的 wordpress 古腾堡中添加语言切换链接,以便我可以轻松切换到相应的语言编辑页面。
我按照以下步骤操作:https://developer.wordpress.org/block-editor/developers/slotfills/plugin-document-setting-panel/。
我的想法是:
- 要获取编辑链接,我们需要知道 post_id。
- 帖子名称在整个网站中是一致的,比如帖子在language1.example.com/post1,然后它有language2.example.com/post1,language3.example.com/post1,等等。 ..
- 因此,我需要先获取帖子名称,然后获取其他语言的帖子 ID,然后将它们保存到链接列表中。
然而,我被困在第 3 步,我不知道如何正确获取帖子名称...
下面是我的代码:
import { registerPlugin } from '@wordpress/plugins';
import { PluginDocumentSettingPanel } from '@wordpress/edit-post';
const { select } = wp.data;
const title = select("core/editor").getEditedPostAttribute( 'title' );
const languageSwitcher = () => (
<PluginDocumentSettingPanel
name="custom-panel"
title="Language Switcher"
className="custom-panel"
>
<a>English {title}</a>
<a>简体中文</a>
<a>繁體中文</a>
</PluginDocumentSettingPanel>
);
registerPlugin( 'language-switcher',{
render: languageSwitcher,icon: 'translation',} );
现在,select("core/editor").getEditedPostAttribute('title') 返回 undefined
...
非常感谢您的帮助!谢谢!
解决方法
您是否尝试过将 wp-data 添加到 wp_enqueue_script.txt 中? 例如:
wp_enqueue_script(
'your-doc',plugins_url( 'build/yourDoc.js',__DIR__ ),[ 'wp-i18n','wp-edit-post','wp-element','wp-data' ],'0.0.1',true
);