JSON API to show Advanced Custom Fields - WordPress

后端 未结 6 2115
被撕碎了的回忆
被撕碎了的回忆 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:50

    I'm not sure if you're still interested in a solution, but I was able to modify the json-api plugin models/post.php file to display the repeater data as an array. This is a modification of a modification made by http://wordpress-problem.com/marioario-on-plugin-json-api-fixed-get-all-custom-fields-the-right-way/

    replace the set_custom_fields_value() function with the following:

    function set_custom_fields_value() {
    
        global $json_api;
    
        if ($json_api->include_value('custom_fields') && $json_api->query->custom_fields) {
    
            // Query string params for this query var
            $params = trim($json_api->query->custom_fields);
    
            // Get all custom fields if true|all|* is passed
            if ($params === "*" || $params === "true" || $params === "all") {
    
                $wp_custom_fields = get_post_custom($this->id);
                $this->custom_fields = new stdClass();
    
                // Loop through our custom fields and place on property
                foreach($wp_custom_fields as $key => $val) {
                    if (get_field($key)) {
                        $this->custom_fields->$key = get_field($key);
                    } else if ($val) {
                        // Some fields are stored as serialized arrays.
                        // This method does not support multidimensionals... 
                        // but didn't see anything wrong with this approach
                        $current_custom_field = @unserialize($wp_custom_fields[$key][0]);
    
                        if (is_array($current_custom_field)) {
    
                            // Loop through the unserialized array
                            foreach($current_custom_field as $sub_key => $sub_val) {
    
                                // Lets append these for correct JSON output
                                $this->custom_fields->$key->$sub_key = $sub_val;
                            }
    
                        } else {
    
                            // Break this value of this custom field out of its array
                            // and place it on the stack like usual
                            $this->custom_fields->$key = $wp_custom_fields[$key][0];
    
                        }
                    }
                }
            } else {
    
                // Well this is the old way but with the unserialized array fix
                $params = explode(',', $params);
                $wp_custom_fields = get_post_custom($this->id);
                $this->custom_fields = new stdClass();
    
                foreach ($params as $key) {
    
                    if (isset($wp_custom_fields[$key]) && $wp_custom_fields[$key][0] ) {
                        $current_custom_field = @unserialize($wp_custom_fields[$key][0]);
    
                        if (is_array($current_custom_field)) {
                            foreach($current_custom_field as $sub_key => $sub_val) {
                                $this->custom_fields->$key->$sub_key = $sub_val;
                            }
    
                        } else {
                            $this->custom_fields->$key = $wp_custom_fields[$key][0];
    
                        }
    
                    }
                }
            }
    
        } else {
            unset($this->custom_fields);
    
        }
    }
    

提交回复
热议问题