Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript(WordPress)中的Gutenberg编辑器内容_Wordpress_Wordpress Gutenberg - Fatal编程技术网

JavaScript(WordPress)中的Gutenberg编辑器内容

JavaScript(WordPress)中的Gutenberg编辑器内容,wordpress,wordpress-gutenberg,Wordpress,Wordpress Gutenberg,回到TMCE时代,我们可以通过editor.getContent()轻松获取编辑器内容。然而,在新的古腾堡编辑器中,我找不到一种方法来做到这一点 我需要所有编辑器的内容作为HTML(它将被保存在数据库中的方式) 我找到了wp.block.serialize()方法,听起来很有希望。但似乎需要块(作为参数)。所以我有点卡住了。你可能想浏览窗口。wpguenbergpost.content它既有原始内容也有渲染内容。这是,目前。事情可能会改变:)从3.1版开始。关于古腾堡,请尝试以下方法: 要获取普

回到TMCE时代,我们可以通过
editor.getContent()
轻松获取编辑器内容。然而,在新的古腾堡编辑器中,我找不到一种方法来做到这一点

我需要所有编辑器的内容作为HTML(它将被保存在数据库中的方式)


我找到了
wp.block.serialize()
方法,听起来很有希望。但似乎需要块(作为参数)。所以我有点卡住了。

你可能想浏览
窗口。wpguenbergpost.content
它既有
原始内容
也有
渲染内容。这是,目前。事情可能会改变:)

从3.1版开始。关于古腾堡,请尝试以下方法:

要获取普通块内容,请执行以下操作:

var originalContent = wp.data.select( "core/editor" ).getCurrentPost().content;
var editedContent = wp.data.select( "core/editor" ).getEditedPostContent();
要渲染帖子(转换为块),请执行以下操作:


您可以使用选择器,它允许我们检索数据,类似地,例如,要更新古腾堡正在编辑的文章的标题,您可以执行以下操作:

wp.data.dispatch( 'core/editor' ).editPost( { title: 'New Title' } );
您可以在这里检查actions文件,以查看由core/editor命名空间定义的操作的完整列表


查看更多信息:

我有类似问题,但无法发表评论。提供的答案流确实设置了文章标题,但从4.5.1开始不处理内容

为了更新帖子内容,我插入了一个段落块。这是我的密码:

wp.data.dispatch( 'core/editor' ).editPost( { title: 'New Title' } );
var editedContent = wp.data.select( "core/editor" ).getEditedPostContent();
var newBlock = wp.blocks.createBlock( "core/paragraph", {
    content: editedContent,
});
wp.data.dispatch( "core/editor" ).insertBlocks( newBlock );

谢谢Andrea,但这似乎返回了保存在数据库中的内容,而不是用户在编辑器中编辑的内容……这似乎过时了。见我的答案。如何编辑内容?我尝试了wp.data.dispatch('core/editor').editPost({content:'\nI love you dadddhhh.

\n'});但它会更新HTML编辑器中的代码,而块不会更新其内容
wp.data.dispatch( 'core/editor' ).editPost( { title: 'New Title' } );
var editedContent = wp.data.select( "core/editor" ).getEditedPostContent();
var newBlock = wp.blocks.createBlock( "core/paragraph", {
    content: editedContent,
});
wp.data.dispatch( "core/editor" ).insertBlocks( newBlock );