检索自定义列上的POST元数据时出现问题

时间:2013-05-17 作者:Behseini

我正在尝试将自定义帖子元数据添加到自定义列。元框有两个文本字段,分别是Price和Company,但当我发布一篇新文章时,它只会在Price列和编辑框中显示Company的值,它还会在Price框中检索Company的值!您能告诉我为什么无法在自定义列中检索到正确的值吗?我是否在保存或检索数据时出错?

add_action(\'admin_init\', \'admin_init\');
add_action(\'save_post\', \'save_options\');
function admin_init(){
add_meta_box(
    "product_options",
    "product",
    "normal",
    "low"
  );
}

function product_opt(){
global $post;
$custom = get_post_custom($post->ID);
$price = $custom[\'price\'][0];
$coMake = $custom[\'coMake\'][0];

echo \'<label style="padding-right:40px;">Price:</label>
    <input name="price" value="\'. $price . \'" style="width:250px;" />\';

echo \'<p/ ><label style="padding-right:40px;">Company:</label>
    <input name="price" value="\'. $coMake . \'" style="width:250px;" />\';    
}

function save_options(){
global $post;
if (!isset($_POST[\'price\']) || $post->post_type != \'product\')
{
    return $post;
}
update_post_meta($post->ID, "price", $_POST[\'price\']);

if (!isset($_POST[\'coMake\']) || $post->post_type != \'product\')
{
    return $post;
}
update_post_meta($post->ID, "coMake", $_POST[\'coMake\']);
    }

          add_filter("manage_edit-product_columns", "edit_col" );
    add_action("manage_posts_custom_column", "custom_col");

    function edit_col($columns){
$columns = array(
    "cb" => "<input type =\'checkbox\' />",
    "title" => "Product Title",
    "price" => "Price",
    "coMake" => "Company",
    "type" => "Product Type" 
);
return $columns;
    }

    function custom_col($column)
    {
global $post;
switch ($column)
{
    case "price":
        $custom = get_post_custom($post->ID);
        echo $custom[\'price\'][0];
        break;
    case "coMake":
        $custom = get_post_custom($post->ID);
        echo $custom[\'coMake\'][0];
        break;
    case "type":
        echo get_the_term_list($post->ID, \'type\', \'\', \', \', \'\');
        break;
}
   }
提前感谢您的评论和帮助

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

您的输入字段具有相同的名称:

<input name="price" ...

结束

相关推荐

将Metabox日期月份编号转换为单词

我使用带有日期(日、月和年)的自定义metabox。问题是,当我尝试将日期数字转换为日期字时,例如10是10月。我使用此代码:function eventposttype_get_the_month_abbr($month) { global $wp_locale; for ( $i = 1; $i < 13; $i = $i +1 ) { if ( $i == $month ) $monthabbr = $wp