Wordpress Gutenberg块验证:块验证失败
我想为搜索帖子创建插件,并将其添加到页面。 但我在储蓄方面有问题Wordpress Gutenberg块验证:块验证失败,wordpress,wordpress-gutenberg,create-guten-block,Wordpress,Wordpress Gutenberg,Create Guten Block,我想为搜索帖子创建插件,并将其添加到页面。 但我在储蓄方面有问题 class mySelectPosts extends Component { static getInitialState() { return { post: {}, }; } constructor() { super( ...arguments ); this.state = this.constructor.getInitialSta
class mySelectPosts extends Component {
static getInitialState() {
return {
post: {},
};
}
constructor() {
super( ...arguments );
this.state = this.constructor.getInitialState();
this.onChangeSearchPost = this.onChangeSearchPost.bind(this);
}
onChangeSearchPost = debounce( 300, async (search) => {
if( search.length < 3 ) {
return
}
let response = await fetch( `/wp-json/wp/v2/search/?search=${encodeURI( search )}&per_page=1` )
let results = await response.json()
let postId = await results[0].id;
let postDataJSON = await fetch( `/wp-json/wp/v2/posts/${postId}` )
let postData = await postDataJSON.json();
this.setState( { post: postData } )
// Set the attributes
this.props.setAttributes( {
title: postData.title.rendered,
content: postData.excerpt.rendered,
link: postData.link,
})
} )
render() {
output = <div className="post">
<a href={ this.state.post.link }><h2 dangerouslySetInnerHTML={ { __html: this.state.post.title.rendered } }></h2></a>
<p dangerouslySetInnerHTML={ { __html: this.state.post.excerpt.rendered } }></p>
</div>;
return [
( <InspectorControls key='inspector'>
<TextControl onChange= { value => this.onChangeSearchPost( value ) } label={ __( 'Select a Post' ) }/>
</InspectorControls>
),
<div className={this.props.className}>{output}</div>
]
}
}
registerBlockType( 'cgb/block-guten-load-post', {
attributes: {
content: {
type: 'string',
source:'html',
selector: 'p'
},
title: {
type: 'string',
selector: 'h2'
},
link: {
type: 'string',
selector: 'a'
},
},
edit: mySelectPosts,
save: ( props ) => {
return (
<div className={ props.className }>
<div className="post">
<a href={ props.attributes.link }><h2 dangerouslySetInnerHTML={ { __html: props.attributes.title } }></h2></a>
<p dangerouslySetInnerHTML={ { __html: props.attributes.content } }></p>
</div>
</div>
);
},
} );
类mySelectPosts扩展组件{
静态getInitialState(){
返回{
职位:{},
};
}
构造函数(){
超级(…参数);
this.state=this.constructor.getInitialState();
this.onChangeSearchPost=this.onChangeSearchPost.bind(this);
}
onChangeSearchPost=debounce(300,异步(搜索)=>{
如果(search.length<3){
返回
}
let response=wait-fetch(`/wp-json/wp/v2/search/?search=${encodeURI(search)}&per_-page=1`)
let results=wait response.json()
让postId=等待结果[0].id;
让postDataJSON=wait-fetch(`/wp-json/wp/v2/posts/${postId}`)
让postData=wait postDataJSON.json();
this.setState({post:postData})
//设置属性
this.props.setAttributes({
标题:postData.title.rendered,
内容:postData.extracpt.rendered,
链接:postData.link,
})
} )
render(){
输出=
;
返回[
(
this.onChangeSearchPost(value)}label={{{uuuu('selecta Post')}/>
),
{output}
]
}
}
registerBlockType('cgb/block guten load post'{
属性:{
内容:{
键入:“字符串”,
资料来源:"html",,
选择器:“p”
},
标题:{
键入:“字符串”,
选择器:“h2”
},
链接:{
键入:“字符串”,
选择器:“a”
},
},
编辑:mySelectPosts,
保存:(道具)=>{
返回(
);
},
} );
当我找到帖子并按下“保存”按钮时,一切正常,但在重新加载后,我有了以下内容。
编辑建议我
我试图改变属性,但它什么也没改变。
更有趣的是,如果我从两侧(保存和渲染)删除“内容”部分,只留下标题,那么WP不会显示错误,也不会在重新加载后保存帖子。
我很高兴听到关于如何修理它的任何想法