douyu9433 2011-05-31 22:59
浏览 50
已采纳

来自表单的数组 - Wordpress元数据

I got a custom post type, with a form for storing some data (name, url) to display in a template.

What I want to know is how can I store those values in an array?

An example of my code:

<? function files_metadata(){  
        global $post; 
        $custom = get_post_custom($post->ID);  
        $name = $custom["name"][0];
        $url = $custom["url"][0];

        echo '<input type="hidden" name="files_metadata" id="files_metadata" value="' .wp_create_nonce('files_m'). '" />'; ?>  

<label>Name: </label><br/>
<input id="name" name="name" value="<?php echo $name; ?>" />
<label>Url: </label><br/>
<input id="url" name="url" value="<?php echo $url; ?>" />

<? function save_meta_files($post_id) {  
        if (!wp_verify_nonce($_POST['files_metadata'], 'files_m')) return $post_id;
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;

        update_post_meta($post_id, "name", $_POST["url"]);   
        update_post_meta($post_id, "url", $_POST["url"]); 
    }  

add_action('save_post', 'save_meta_files'); ?> 

To this I want to add something like...

$url = $custom["url"][0];
$url2 = $custom["url"][1];
$url3 = $custom["url"][2];

<input id="url" name="url[0]" value="<?php echo $url; ?>" />
<input id="url2" name="url[1]" value="<?php echo $url2; ?>" />
<input id="url3" name="url[2]" value="<?php echo $url3; ?>" />

update_post_meta($post_id, "url", $_POST["url"][0]); 
update_post_meta($post_id, "url2", $_POST["url"][1]); 
update_post_meta($post_id, "url3", $_POST["url"][2]); 

...but that actually works...

  • 写回答

1条回答 默认 最新

  • duanqiang2977 2011-06-01 16:23
    关注

    A passed array will be serialized into a string: http://codex.wordpress.org/Function_Reference/update_post_meta

    update_post_meta(
        $post_id,
        'files_metadata',
        array(
            'name1' => $_POST['name1'],
            'url1' => $_POST['url1']
            'name2' => $_POST['name2'],
            'url2' => $_POST['url2']
        )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀