gutenberg 块中的 setMeta 将元值保存在多行而不是一行
setMeta in gutenberg block saves the meta value in multiple rows instead of one row
我正在编写一个将数据保存到元字段中的古腾堡块。问题是该值被保存在数据库中的多行而不是一行中。如何保存一行?
例如:'My apple'保存为metakeyname:my和metakeyname:apple存入数据库。我想将它保存为 metakeyname: My apple
下面是我的代码。请帮忙。谢谢!
const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const { RichText, PlainText, MediaUpload, InspectorControls } = wp.editor;
const { TextControl, Tooltip, PanelBody, PanelRow, FormToggle, Button } = wp.components;
const el = wp.element.createElement;
registerBlockType('my/fields', {
title: 'My Fields',
description: 'Fill in the required data and press the Update button above',
icon: 'products',
category: 'common',
attributes: {
sku: {type: 'string'},
pdf: {type: 'string'},
gallery: {type: 'string'},
features: {type: 'string'},
brands: {type: 'string'},
category: {type: 'string'},
},
edit: (props) => {
const { attributes, setAttributes } = props;
const [meta, setMeta] = wp.coreData.useEntityProp('postType', 'post', 'meta');
return el(
"div",
null,
el(
"h3",
null,
"Please enter Product name above & details below"
),
el(TextControl, {
label: "SKU:",
value: attributes.sku,
onChange: (newtext) => setAttributes({ sku: newtext }) & setMeta({sku: newtext})
})
);
},
save: function(props) {
return props.attributes.sku;
}
})
我终于明白了。我必须定义 single = true 和类型,同时使特定字段在其余部分可用 api.
// Meta in REST API
add_action( 'rest_api_init', 'my_register_meta_fields');
function my_register_meta_fields() {
register_meta( 'post', 'sku', array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
));
}
我正在编写一个将数据保存到元字段中的古腾堡块。问题是该值被保存在数据库中的多行而不是一行中。如何保存一行?
例如:'My apple'保存为metakeyname:my和metakeyname:apple存入数据库。我想将它保存为 metakeyname: My apple
下面是我的代码。请帮忙。谢谢!
const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const { RichText, PlainText, MediaUpload, InspectorControls } = wp.editor;
const { TextControl, Tooltip, PanelBody, PanelRow, FormToggle, Button } = wp.components;
const el = wp.element.createElement;
registerBlockType('my/fields', {
title: 'My Fields',
description: 'Fill in the required data and press the Update button above',
icon: 'products',
category: 'common',
attributes: {
sku: {type: 'string'},
pdf: {type: 'string'},
gallery: {type: 'string'},
features: {type: 'string'},
brands: {type: 'string'},
category: {type: 'string'},
},
edit: (props) => {
const { attributes, setAttributes } = props;
const [meta, setMeta] = wp.coreData.useEntityProp('postType', 'post', 'meta');
return el(
"div",
null,
el(
"h3",
null,
"Please enter Product name above & details below"
),
el(TextControl, {
label: "SKU:",
value: attributes.sku,
onChange: (newtext) => setAttributes({ sku: newtext }) & setMeta({sku: newtext})
})
);
},
save: function(props) {
return props.attributes.sku;
}
})
我终于明白了。我必须定义 single = true 和类型,同时使特定字段在其余部分可用 api.
// Meta in REST API
add_action( 'rest_api_init', 'my_register_meta_fields');
function my_register_meta_fields() {
register_meta( 'post', 'sku', array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
));
}