我已经创建了一个块,需要将面板上某个操作的值另存为post\\u meta。
Php端:
add_action( \'init\', \'yasr_gutenberg_show_in_rest_overall_meta\' );
function yasr_gutenberg_show_in_rest_overall_meta() {
register_meta( \'post\', \'yasr_overall_rating\',
array(
\'show_in_rest\' => true,
\'single\' => true,
\'type\' => \'number\',
)
);
}
然后在js中,我会这样做:
attributes: {
//name of the attribute
overallRating: {
type: \'number\',
source: \'meta\',
meta: \'yasr_overall_rating\'
},
}
edit:
function( props ) {
const { attributes: {overallRating}, setAttributes, isSelected } = props;
/***
Action and ajax action to update metadata
and where I use setAttributes to
update overallRating
***/
}
一切似乎都正常,但当保存或更新帖子时,总体评分会再次获得初始值,而不是通过ajax调用保存的新值。
我怎样才能做到这一点?