JSON API to show Advanced Custom Fields - WordPress

后端 未结 6 2008
被撕碎了的回忆
被撕碎了的回忆 2021-01-30 15:14

I am developing a magazine WordPress site that will have a json feed for a Mobile App. I set the backend up using Advanced Custom Fields with a Repeater Field for Multiple Artic

6条回答
  •  盖世英雄少女心
    2021-01-30 15:54

    @Myke: you helped me tremendously. Here's my humble addition:

    add_filter('json_api_encode', 'json_api_encode_acf');
    
    
    function json_api_encode_acf($response) 
    {
        if (isset($response['posts'])) {
            foreach ($response['posts'] as $post) {
                json_api_add_acf($post); // Add specs to each post
            }
        } 
        else if (isset($response['post'])) {
            json_api_add_acf($response['post']); // Add a specs property
        }
    
        return $response;
    }
    
    function json_api_add_acf(&$post) 
    {
        $post->acf = get_fields($post->id);
    }
    

提交回复
热议问题