我从这个链接获得了这个问题的近似解决方案”How to add a textarea to only one of the fields of this custom metabox?,但使用不同的代码,他使用新的metabox,我还需要查看自定义字段上的值,使用此解决方案,我无法在“自定义字段框”上看到。
因此,我使用以下代码:
$sp_boxes = array (
\'Digi Box\' => array (
array( \'shortdesc\', \'Shortdesc:\' ),
array( \'url\', \'Url:\' ),
),
);
add_action( \'admin_menu\', \'sp_add_custom_box\' );
add_action( \'save_post\', \'sp_save_postdata\', 1, 2 );
function sp_add_custom_box() {
global $sp_boxes;
if ( function_exists( \'add_meta_box\' ) ) {
foreach ( array_keys( $sp_boxes ) as $box_name ) {
add_meta_box( $box_name, __( $box_name, \'sp\' ), \'sp_post_custom_box\', \'post\', \'normal\', \'high\' );
}
}
}
function sp_post_custom_box ( $obj, $box ) {
global $sp_boxes;
static $sp_nonce_flag = false;
if ( ! $sp_nonce_flag ) {
echo_sp_nonce();
$sp_nonce_flag = true;
}
foreach ( $sp_boxes[$box[\'id\']] as $sp_box ) {
echo field_html( $sp_box );
}
}
function field_html ( $args ) {
switch ( $args[2] ) {
case \'textarea\':
return text_area( $args );
case \'checkbox\':
// To Do
case \'radio\':
// To Do
case \'text\':
default:
return text_field( $args );
}
}
function text_field ( $args ) {
global $post;
$args[2] = get_post_meta($post->ID, $args[0], true);
$args[1] = __($args[1], \'sp\' );
$label_format =
\'<label for="%1$s" style="font-size: 15px; font-weight: bold;">%2$s</label><input style="float: right; margin: -2px 0 0; width: 88%%;" type="text" name="%1$s" value="%3$s" /><br /><br /><br />\';
return vsprintf( $label_format, $args );
}
function sp_save_postdata($post_id, $post) {
global $sp_boxes;
if ( ! wp_verify_nonce( $_POST[\'sp_nonce_name\'], plugin_basename(__FILE__) ) ) {
return $post->ID;
}
if ( \'page\' == $_POST[\'post_type\'] ) {
if ( ! current_user_can( \'edit_page\', $post->ID ))
return $post->ID;
} else {
if ( ! current_user_can( \'edit_post\', $post->ID ))
return $post->ID;
}
foreach ( $sp_boxes as $sp_box ) {
foreach ( $sp_box as $sp_fields ) {
$my_data[$sp_fields[0]] = $_POST[$sp_fields[0]];
}
}
foreach ($my_data as $key => $value) {
if ( \'revision\' == $post->post_type ) {
return;
}
$value = implode(\',\', (array)$value);
if ( get_post_meta($post->ID, $key, FALSE) ) {
update_post_meta($post->ID, $key, $value);
} else {
add_post_meta($post->ID, $key, $value);
}
if (!$value) {
delete_post_meta($post->ID, $key);
}
}
}
function echo_sp_nonce () {
echo sprintf(
\'<input type="hidden" name="%1$s" id="%1$s" value="%2$s" />\',
\'sp_nonce_name\',
wp_create_nonce( plugin_basename(__FILE__) )
);
}
我有两个字段:1。短描述2。url地址
使用此代码,它只显示输入类型。现在,如何使“shortdesc”成为textarea类型?和“url”仍然是输入类型。
谢谢
对不起,我的英语不好。
最合适的回答,由SO网友:dimsdims 整理而成
我找到了解决方案,
array( \'shortdesc\', \'shortdesc\' , \'textarea\'),
并为text\\u区域添加新功能:
function text_area ( $args ) {
global $post;
// adjust data
$args[2] = get_post_meta($post->ID, $args[0], true);
$args[1] = __($args[1], \'fp\' );
$label_format =
\'<label for="%1$s">%2$s</label><br />\' . \'<textarea style="width: 95%%;" name="%1$s">%3$s</textarea><br /><br />\';
return vsprintf( $label_format, $args );
}