在我的wordpress主题中有一个名为歌曲的自定义帖子类型,在该帖子类型中有一个名为播放列表的自定义字段,其中包含歌曲信息,如艺术家姓名,下载链接,歌词等...
我的问题是这个自定义字段是在我网站的json版本中序列化的 我不知道如何解决这个问题。
我使用json api plugin进行文字输入。
这是一个在json中显示的例子
...
"custom_fields": {
...
"playlist": [
"a:1:{i:0;a:19:{s:5:\"title\";s:44:\"Hosein Tohi And Sami Beigi - Ba Man Miraghsi\";s:3:\"mp3\";s:134:\"http:\/\/dl.paradi3emusic.com\/Musics\/Aban%2094\/Persian\/Single\/Hosein%20Tohi%20And%20Sami%20Beigi%20-%20Ba%20Man%20Miraghsi%20-%20128.mp3\";s:7:\"radioip\";s:0:\"\";s:9:\"radioport\";s:0:\"\";s:11:\"buy_title_a\";s:14:\"\u06a9\u06cc\u0641\u06cc\u062a 320\";s:10:\"buy_icon_a\";s:14:\"cloud-download\";s:10:\"buy_link_a\";s:134:\"http:\/\/dl.paradi3emusic.com\/Musics\/Aban%2094\/Persian\/Single\/Hosein%20Tohi%20And%20Sami%20Beigi%20-%20Ba%20Man%20Miraghsi%20-%20320.mp3\";s:11:\"buy_title_b\";s:14:\"\u06a9\u06cc\u0641\u06cc\u062a 128\";s:10:\"buy_icon_b\";s:14:\"cloud-download\";s:10:\"buy_link_b\";s:134:\"http:\/\/dl.paradi3emusic.com\/Musics\/Aban%2094\/Persian\/Single\/Hosein%20Tohi%20And%20Sami%20Beigi%20-%20Ba%20Man%20Miraghsi%20-%20128.mp3\";s:11:\"buy_title_c\";s:0:\"\";s:10:\"buy_icon_c\";s:14:\"cloud-download\";s:10:\"buy_link_c\";s:0:\"\";s:11:\"buy_title_d\";s:0:\"\";s:10:\"buy_icon_d\";s:14:\"cloud-download\";s:10:\"buy_link_d\";s:0:\"\";s:10:\"buy_custom\";s:0:\"\";s:11:\"lyric_title\";s:0:\"\";s:5:\"lyric\";s:0:\"\";}}"
],
...
},
...
**** ***** EDIT
我想要另一个自定义字段,其中包含这些序列化数据的json数组但未反序列化 我试过这个但是没有用。
add_post_meta($id, 'myplaylist1', $playlist);
答案 0 :(得分:2)
在json api插件输出内容之前,您需要编写一个过滤器来反序列化内容。
在输出json之前插件应用了一个过滤器,在此处记录:
https://en-gb.wordpress.org/plugins/json-api/other_notes/#5.-Extending-JSON-API
这适合这项任务。
将以下内容添加到主题functions.php
:
add_filter('json_api_encode', function($response){
if (isset($response['posts'])) {
foreach ($response['posts'] as $post) {
deserialize_playlist($post);
}
} else if (isset($response['post'])) {
deserialize_playlist($response['post']);
}
return $response;
});
function deserialize_playlist(&$post) {
if(isset($post->custom_fields->playlist)){
$playlists = $post->custom_fields->playlist;
//custom fields appear to always be returned as an array
foreach($playlists as &$playlist){
$playlist = unserialize($playlist);
}
$post->custom_fields->playlist = $playlists;
}
}
答案 1 :(得分:0)
您可以使用maybe_unserialize()函数中内置的wordpress来反序列化序列化数据
https://codex.wordpress.org/Function_Reference/maybe_unserialize