我有一个保存在postmata中的数组,每个数组键都成为一个元键。我想更改代码以使用一个元键保存整个数组。怎么做?谢谢!

$poddata = Array(
'pod_id' => $this->pod_id,
'url' => $this->url,
'name' => $this->name,
'description' => $this->description,
'service' => $this->service,
'status' =>$this->status,
'price' => $this->price
);

foreach ( $poddata as $k => $v ){

if ( get_post_meta( $this->id, $k ) == '' )
add_post_meta( $this->id, $meta_box, $v, true );

elseif ( $v != get_post_meta( $this->id, $k, true ) )
update_post_meta( $this->id, $k, $v );

elseif ( $v == '' )
delete_post_meta( $this->id, $k, get_post_meta( $this->id, $k, true ) );

}


#1 楼

您不需要遍历这些值。只需使用update_post_meta($post_ID, {key}, {array of vals}),就可以了!

<?php
$poddata = Array(
    'pod_id' => $this->pod_id,
    'url' => $this->url,
    'name' => $this->name,
    'description' => $this->description,
    'service' => $this->service,
    'status' =>$this->status,
    'price' => $this->price
    );

//Update inserts a new entry if it doesn't exist, updates otherwise
update_post_meta($post_ID, 'poddata', $poddata);
?>


就这样!当您获取它以供使用时,请执行以下操作:

    $poddata = get_post_meta($post_ID, 'poddata');


$ poddata是值的数组。

评论


我尝试了update_post_meta($ post_ID,'poddata',$ postdata),在保存帖子后,我看到未保存元。

–珍妮
2011年8月6日在8:49

哦,很抱歉,它已保存,我在WP自定义字段表中没有看到它。我刚刚在phpAdmin中找到了它。谢谢!

–珍妮
2011年8月6日上午8:59

检索数据时,通过get_post_meta($ post_ID,'poddata');我从var_dump获得array(0)。我如何获得整个阵列?

–珍妮
11年8月6日在9:28

别客气!尝试使用print_r()... echo“
”; print_r($ poddata);回显“ 
”;

–Rutwick Gangurde
2011年8月6日9:30



print_r($ poddata)显示Array()

–珍妮
11年8月6日在9:47