Php WP Metabox未保存后期元数据
我试图在页面上实现一个简单的复选框,以便在用户选择的情况下动态添加Html块,但我无法保存post_meta以执行此任务,有人可以帮助我吗?此复选框输入的值未保存在post元信息中 这就是我到目前为止在functions.php上得到的结果Php WP Metabox未保存后期元数据,php,wordpress,meta-boxes,post-meta,Php,Wordpress,Meta Boxes,Post Meta,我试图在页面上实现一个简单的复选框,以便在用户选择的情况下动态添加Html块,但我无法保存post_meta以执行此任务,有人可以帮助我吗?此复选框输入的值未保存在post元信息中 这就是我到目前为止在functions.php上得到的结果 function wporg_add_custom_box(){ $screens = ['page', 'wporg_cpt']; foreach ($screens as $screen) { add_meta_box(
function wporg_add_custom_box(){
$screens = ['page', 'wporg_cpt'];
foreach ($screens as $screen) {
add_meta_box(
'wporg_box_id', // Unique ID
'Entra in Flee Block', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
$screen, // Post type
'side'
);
}
}
add_action('add_meta_boxes', 'wporg_add_custom_box');
function wporg_custom_box_html($post){
$value = get_post_meta($post->ID, '_wporg_meta_key', true);
?>
<label for="wporg_field">Add "Entra in Flee" block to page</label>
</br>
<input type="checkbox" name="wporg_field" id="wporg_field" class="postbox">
<?php
}
function wporg_save_postdata($post_id){
if (array_key_exists('wporg_field', $_POST)) {
update_post_meta(
$post_id,
'_wporg_meta_key',
$_POST['wporg_field']
);
}
}
add_action('save_post', 'wporg_save_postdata');
函数worg\u add\u custom\u box(){
$screens=['page','worg\u cpt'];
foreach($screens作为$screen){
添加元框(
“worg\u box\u id”,//唯一id
“进入逃逸区块”,//方框标题
“wporg\u custom\u box\u html”//内容回调必须是可调用的类型
$screen,//Post类型
“一边”
);
}
}
添加操作(“添加元框”、“wporg添加自定义框”);
函数worg\u custom\u box\u html($post){
$value=get_post_meta($post->ID,'wporg_meta_key',true);
?>
在页面中添加“进入逃逸”块
在wp\u cusotm\u box\u html函数中不使用$value。
我想应该是这样的:
function wporg_add_custom_box()
{
$screens = ['page', 'wporg_cpt'];
foreach ($screens as $screen) {
add_meta_box(
'wporg_box_id', // Unique ID
'Entra in Flee Block', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
$screen, // Post type
'side'
);
}
}
add_action('add_meta_boxes', 'wporg_add_custom_box');
function wporg_custom_box_html($post)
{
$value = get_post_meta($post->ID, '_wporg_meta_key', true) ? 'checked' : '';
?>
<label for="wporg_field">Add "Entra in Flee" block to page</label>
</br>
<input type="checkbox" name="wporg_field" id="wporg_field" class="postbox" <?php echo $value; ?>>
<?php
}
function wporg_save_postdata($post_id)
{
if (array_key_exists('wporg_field', $_POST)) {
update_post_meta(
$post_id,
'_wporg_meta_key',
$_POST['wporg_field']
);
} else {
delete_post_meta(
$post_id,
'_wporg_meta_key'
);
}
}
add_action('save_post', 'wporg_save_postdata');
函数worg\u add\u custom\u box()
{
$screens=['page','worg\u cpt'];
foreach($screens作为$screen){
添加元框(
“worg\u box\u id”,//唯一id
“进入逃逸区块”,//方框标题
“wporg\u custom\u box\u html”//内容回调必须是可调用的类型
$screen,//Post类型
“一边”
);
}
}
添加操作(“添加元框”、“wporg添加自定义框”);
函数worg\u custom\u box\u html($post)
{
$value=get_post_meta($post->ID,'wporg_meta_key',true)?“选中”:“;
?>
在页面中添加“进入逃逸”块
复选框不检查我是否做了,保存页面。关于可能发生什么的任何想法?是的,代码只更新Pytheta Meta,如果复选框被选中。我更新了我的答案,在没有检查的情况下考虑。