如何将文本区域仅添加到此自定义Metabox的一个字段?

时间:2011-04-10 作者:janoChen

这是我用来创建元框的代码:

<?php

$key = "project";
$meta_boxes = array(
    "project_services" => array(
        "name" => "project_services",
        "title" => "Services",
        "description" => "List the services provided for the project."),
    "project_name" => array(
        "name" => "project_name",
        "title" => "Name",
        "description" => "Write the name of the project."),
    "project_overview" => array(
        "name" => "project_overview",
        "title" => "Overview",
        "description" => "Write an overview of the project.")
    );

function create_meta_box() {
    global $key;

    if( function_exists( \'add_meta_box\' ) ) {
        add_meta_box( \'new-meta-boxes\', ucfirst( $key ) . \' Description\', \'display_meta_box\', \'post\', \'normal\', \'high\' );
    }
}

function display_meta_box() {
    global $post, $meta_boxes, $key;
?>

<div class="form-wrap">

<?php
    wp_nonce_field( plugin_basename( __FILE__ ), $key . \'_wpnonce\', false, true );

    foreach($meta_boxes as $meta_box) {
        $data = get_post_meta($post->ID, $key, true);
?>

<div class="form-field form-required">
<label for="<?php echo $meta_box[ \'name\' ]; ?>"><?php echo $meta_box[ \'title\' ]; ?></label>
<input type="text" name="<?php echo $meta_box[ \'name\' ]; ?>" value="<?php echo htmlspecialchars( $data[ $meta_box[ \'name\' ] ] ); ?>" />
<p><?php echo $meta_box[ \'description\' ]; ?></p>
</div>

<?php } ?>

</div>
<?php
}

function save_meta_box( $post_id ) {
    global $post, $meta_boxes, $key;

    foreach( $meta_boxes as $meta_box ) {
        $data[ $meta_box[ \'name\' ] ] = $_POST[ $meta_box[ \'name\' ] ];
    }

    if ( !wp_verify_nonce( $_POST[ $key . \'_wpnonce\' ], plugin_basename(__FILE__) ) )
        return $post_id;

    if ( !current_user_can( \'edit_post\', $post_id ))
        return $post_id;

    update_post_meta( $post_id, $key, $data );
}

add_action( \'admin_menu\', \'create_meta_box\' );
add_action( \'save_post\', \'save_meta_box\' );

?>
然后,我将输入更改为文本区域:

<textarea name="<?php echo $meta_box[ \'name\' ]; ?>">
<?php echo htmlspecialchars( $data[ $meta_box[ \'name\' ] ] ); ?>
</textarea>
这会将所有三个字段转换为文本区域。我只想做第三个领域,project_overview文本区域。有什么建议吗?

1 个回复
最合适的回答,由SO网友:keatch 整理而成

使用类似以下内容修改数组定义,以添加指定字段类型的数组新元素

"project_overview" => array(
    "name" => "project_overview",
    "title" => "Overview",
    "description" => "Write an overview of the project.",
    "type"=>"textarea")
然后在代码中替换

<input type="text" name="<?php echo $meta_box[ \'name\' ]; ?>" value="<?php echo htmlspecialchars( $data[ $meta_box[ \'name\' ] ] ); ?>" />
用这样的东西

<? if ( $meta_box[\'type\'] == \'textarea\') { ?
   <textarea name="<?php echo $meta_box[ \'name\' ]; ?>">
   <?php echo htmlspecialchars( $data[ $meta_box[ \'name\' ] ] ); ?>
   </textarea> 
} 
else  { ?>
   <input type="text" name="<?php echo $meta_box[ \'name\' ]; ?>" 
   value="<?php echo      htmlspecialchars( $data[ $meta_box[ \'name\' ] ] ); ?>" />
} ?>
最好将上面的代码片段封装在函数中,如

function print_meta_box ( $meta_box ) {
  switch (meta_box [\'type\'] ) {
      case \'textarea\':
            .... your code ...
            break;
      default:
  }  
}

结束

相关推荐

如何在admin中过滤摘录的metabox描述?

我想更改默认设置“摘录是可选的手工制作的内容摘要,可用于主题。了解有关手动摘录的更多信息。”摘录输入区域下方的帮助文本,对我的自定义帖子类型更有意义。我已经使用“翻译”过滤器对帖子标题做了类似的操作,但如何处理帖子摘录?这是我当前的代码: add_filter(\'gettext\', \'custom_rewrites\', 10, 4); function custom_rewrites($translation, $text, $domain) { global $post;&