在angularjs中使用wordpress REST api时,将wordpress元数据反序列化

时间:2017-12-25 16:09:30

标签: wordpress wordpress-rest-api

我使用wordpress rest api从wordpress到angularjs应用程序获取元数据。如何在离开服务器之前反序列化wordpress元数据unserialize。元数据是这样的:

["a:29:{s:8:\"subtitle\";s:14:\"iphone Showroom\";s:12:\"featuredItem\";s:1:\"0\";s:10:\"headerType\";s:3:\"map\";s:11:\"headerImage\";s:0:\"\";s:12:\"headerHeight\";s:0:\"\";s:3:\"map\";a:7:{s:7:\"address\";s:23:\"Mod city\";s:8:\"latitude\";s:13:\"26.4819543403\";s:9:\"longitude\";s:13:\"76.7334592342\";s:10:\"streetview\";s:1:\"0\";s:9:\"swheading\";s:2:\"90\";s:7:\"swpitch\";s:1:\"5\";s:6:\"swzoom\";s:1:\"1\";}s:9:\"telephone\";s:12:\"074002344777\";s:19:\"telephoneAdditional\";s:0:\"\";s:5:\"email\";s:26:\"shatrrs@gmail.com\";s:9:\"showEmail\";s:1:\"1\";s:15:\"contactOwnerBtn\";s:1:\"1\";s:3:\"web\";s:0:\"\";s:12:\"webLinkLabel\";s:0:\"\";s:19:\"displayOpeningHours\";s:1:\"1\";s:18:\"openingHoursMonday\";s:19:\"09:00 AM - 08:00 PM\";s:19:\"openingHoursTuesday\";s:19:\"09:00 AM - 08:00 PM\";s:21:\"openingHoursWednesday\";s:19:\"09:00 AM - 08:00 PM\";s:20:\"openingHoursThursday\";s:19:\"09:00 AM - 08:00 PM\";s:18:\"openingHoursFriday\";s:19:\"09:00 AM - 08:00 PM\";s:20:\"openingHoursSaturday\";s:19:\"09:00 AM - 08:00 PM\";s:18:\"openingHoursSunday\";s:19:\"09:00 AM - 08:00 PM\";s:16:\"openingHoursNote\";s:0:\"\";s:18:\"displaySocialIcons\";s:1:\"0\";s:26:\"socialIconsOpenInNewWindow\";s:1:\"0\";s:11:\"socialIcons\";s:0:\"\";s:14:\"displayGallery\";s:1:\"1\";s:7:\"gallery\";a:2:{i:0;a:2:{s:5:\"title\";s:0:\"\";s:5:\"image\";s:68:\"http://www.example.com/wp-content/uploads/2017/02/DSCN0366-min.jpg\";}i:1;a:2:{s:5:\"title\";s:0:\"\";s:5:\"image\";s:68:\"http://www.example.com/wp-content/uploads/2017/02/DSCN0365-min.jpg\";}}s:15:\"displayFeatures\";s:1:\"0\";s:8:\"features\";s:0:\"\";}"]

2 个答案:

答案 0 :(得分:0)

unserialize()服务器端,然后json_encode()发送到客户端

$output = json_encode(unserialize($serializedString));

或者如果你是序列化它的人,不要......只是json_encode它

答案 1 :(得分:0)

我做了一些研究并找到了解决方案。我们必须在服务器端这样做。 你应该编辑你的function.php并使用像这样的代码

add_action( 'rest_api_init', 'create_api_custom_meta_field' );

function create_api_custom_meta_field() {
    register_rest_field( 'custom-post', 'post_all_meta_fields', array(
        'get_callback' => 'get_post_meta_for_api',
        'schema' => null,
         ));
}


function get_post_meta_for_api( $object ) {
    //get the id of the post object array
    $post_id = $object['id'];

    //return the post meta
    return get_post_meta( $post_id );
}

在此代码之后,您将获得json对象中的所有序列化元数据。 :)