问题描述
我想为搜索帖子创建插件并将其添加到页面。 但是我在保存时遇到了问题。
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>
);
},} );
当我发现发布并按“保存”后一切正常,但是重新加载后我关注了errors。 然后编辑建议我change my block
我尝试更改属性,但没有任何改变。 更有趣的是,如果我从两侧(保存和渲染)都删除了“内容”部分,只保留了标题,那么WP在重新加载后不会显示错误,也不会保存帖子。 我很高兴听到有关如何解决它的任何想法
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)