我最近从themeforest购买了一个主题,它有一个自定义的帖子类型。以下是指向使用自定义帖子类型创建的页面的链接:
http://bluepresley.com/farmerfinder/?dir-item=dance-relax
自定义post类型有大约20个不同的自定义字段,但您可以从我的数组转储中看到,one one字段已保存为序列化数据:
(使用get\\u post\\u custom()进行数组转储)
Array
(
[_edit_last] => Array
(
[0] => 1
)
[_ait-dir-item] => Array
(
[0] => a:20:{s:7:"address";s:51:"276 Canal Street, New York, NY 10013, United States";s:11:"gpsLatitude";s:17:"40.71958306715651";s:12:"gpsLongitude";s:17:"-74.0379810333252";s:14:"showStreetview";a:1:{s:6:"enable";s:6:"enable";}s:18:"streetViewLatitude";s:9:"40.719679";s:19:"streetViewLongitude";s:18:"-74.03797400000002";s:17:"streetViewHeading";s:18:"-151.8987176681924";s:15:"streetViewPitch";s:17:"5.196265482127373";s:14:"streetViewZoom";s:1:"0";s:9:"telephone";s:13:"044 802 52578";s:5:"email";s:19:"[email protected]";s:3:"web";s:17:"www.aitthemes.com";s:11:"hoursMonday";s:9:"8am - 8pm";s:12:"hoursTuesday";s:9:"8am - 8pm";s:14:"hoursWednesday";s:9:"8am - 8pm";s:13:"hoursThursday";s:9:"8am - 8pm";s:11:"hoursFriday";s:9:"8am - 8pm";s:13:"hoursSaturday";s:6:"closed";s:11:"hoursSunday";s:6:"closed";s:18:"alternativeContent";s:33:"What goes in alternative content?";}
)
[_thumbnail_id] => Array
(
[0] => 8159
)
[_edit_lock] => Array
(
[0] => 1409208357:1
)
[slide_template] => Array
(
[0] =>
)
)
请注意,有问题的密钥是\\u ait-dir-item。
我注意到在表单本身中,所有表单字段都被命名为name="_ait-dir-item[address]"
(例如)。
如果是这种情况,如何以编程方式添加项?这是典型的Wordpress吗?有没有办法处理这种方法?我想我不能使用add_post_meta( $post_id, \'mymetakey\', $my_long );
或找到其他方法here 和here. ?
SO网友:HU ist Sebastian
要在Wordpress中序列化和取消序列化数据,可以使用maybe_unserialize 和maybe_serialize. 如果要处理数据并添加/更改值,可以使用以下方法:
$metaarray = maybe_unserialize(get_post_meta($post_id,\'_ait-dir-item\',TRUE));
//now $metaarray has an array, if it is serialized. If not, $metaarray has a string.
if(is_array($metaarray)){
$metaarray[\'mynewfield\'] = \'Some text i need to add\';
$metaarray[\'mysecondnewfield\'] = array(\'an\' => \'array\',\'i\' => \'need\', \'to\' => \'add\');
}
update_post_meta($post_id,\'_ait-dir-item\',maybe_serialize($metaarray));
快乐编码,Kuchenundkakao