我在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 ) );
}
最合适的回答,由SO网友:Rutwick Gangurde 整理而成
您不需要遍历这些值。仅使用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是值的数组。