我目前正在创建一个自定义帖子类型,我想添加一些自定义元框。除了一个复选框字段,所有字段都按预期运行,它在回显隐藏字段后打印在html checked=\'checked\'上,我不知道为什么。我试着不回应这个值或引用,但还是发生了。这是我的代码:
//Add Zoom Activity Status
add_meta_box(
\'zact_zoom_activity_state\',
\'Zoom Activity Comming Soon\',
\'zact_zoom_activity_status\',
\'zoom-activity\',
\'advanced\',
\'core\'
);
function zact_zoom_activity_status ()
{
global $post;
// Get the location data if it\'s already been entered
$zoom_activity_state = get_post_meta( $post->ID, \'zoom_activity_state\', true );
// Nonce field to validate form request came from current site
wp_nonce_field( plugin_basename(__FILE__),\'zoom_room_state\') ;
// Output the field
echo \'<lable for="zoom_activity_state"> <input type="checkbox" name="zoom_activity_state" value="\' . $zoom_activity_state . \'" class="widefat" \' . checked($zoom_activity_state) . \'/> Is comming soon?</lable>\';
}
这是我得到的不想要的结果:
这是保存代码:
// Verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times. The checked value is being retrived from the nonce name field.
if ( ! isset( $_POST[\'zoom_room_state\'] ) || ! wp_verify_nonce( $_POST[\'zoom_room_state\'], plugin_basename(__FILE__) ) )
{
return $post_id;
}
// If the field is a checkbox we need to check if the value is not null and assing a boolean value to be compared when setting the status.
if( isset( $_POST[\'zoom_activity_state\'] ))
{
$zoom_activity_meta[\'zoom_activity_state\'] = true;
}
else
{
$zoom_activity_meta[\'zoom_activity_state\'] = false;
}//循环浏览zoom\\u activity\\u元数组。//注意,在本例中,我们只有一个项目,但如果您有多个项目,这将非常有用。foreach($zoom\\u activity\\u meta as$键=>;$值):
// Don\'t store custom data twice
if ( \'revision\' === $post->post_type ) {
return;
}
if ( get_post_meta( $post_id, $key, true ) ) {
// If the custom field already has a value, update it.
update_post_meta( $post_id, $key, $value );
} else {
// If the custom field doesn\'t have a value, add it.
add_post_meta( $post_id, $key, $value);
}
if ( ! $value ) {
// Delete the meta key if there\'s no value
delete_post_meta( $post_id, $key );
}
endforeach;
我真不知道我做错了什么。我查阅了wordpress文档和互联网上的其他文章,但在这方面没有明确的内容。有什么线索吗?谢谢
最合适的回答,由SO网友:Jacob Peattie 整理而成
这是因为您使用了checked()
函数,它在另一个echo语句中回显其值。如果要向字符串添加值,则需要函数return
a值。这可以通过设置第三个参数来实现,$echo
, 到false
:
echo \'<lable for="zoom_activity_state"> <input type="checkbox" name="zoom_activity_state" value="\' . $zoom_activity_state . \'" class="widefat" \' . checked($zoom_activity_state, true, false) . \'/> Is comming soon?</lable>\';
您还拼写了
<label>
错误:
echo \'<label for="zoom_activity_state"> <input type="checkbox" name="zoom_activity_state" value="\' . $zoom_activity_state . \'" class="widefat" \' . checked($zoom_activity_state, true, false) . \'/> Is comming soon?</label>\';