Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于显示高级自定义字段的JSON API-WordPress_Json_Wordpress - Fatal编程技术网

用于显示高级自定义字段的JSON API-WordPress

用于显示高级自定义字段的JSON API-WordPress,json,wordpress,Json,Wordpress,我正在开发一个WordPress杂志网站,该网站将为移动应用程序提供json提要。我使用高级自定义字段设置后端,并为多篇文章和每篇文章中的多个页面设置一个Repeater字段。 我使用的是JSON API,但它不包括任何自定义字段。目前是否有一个插件可以做到这一点 是通过搜索同一个问题来到这里的。这还没有完全审查,但我认为这是在正确的道路上。看看吧 我的嵌套级别比你的少一个,所以这可能需要修改一点。但是JSON API插件有一个名为JSON_API_encode的过滤器。我有一个名为规范的中继

我正在开发一个WordPress杂志网站,该网站将为移动应用程序提供json提要。我使用高级自定义字段设置后端,并为多篇文章和每篇文章中的多个页面设置一个Repeater字段。

我使用的是JSON API,但它不包括任何自定义字段。目前是否有一个插件可以做到这一点


是通过搜索同一个问题来到这里的。这还没有完全审查,但我认为这是在正确的道路上。看看吧

我的嵌套级别比你的少一个,所以这可能需要修改一点。但是JSON API插件有一个名为JSON_API_encode的过滤器。我有一个名为规范的中继器,看起来像这样

在我的函数文件中,我有这个

add_filter('json_api_encode', 'my_encode_specs');

function my_encode_specs($response) {
  if (isset($response['posts'])) {
    foreach ($response['posts'] as $post) {
      my_add_specs($post); // Add specs to each post
    }
  } else if (isset($response['post'])) {
    my_add_specs($response['post']); // Add a specs property
  }
  return $response;
}

function my_add_specs(&$post) {
  $post->specs = get_field('specifications', $post->id);
}
将自定义值附加到JSON API输出。请注意,ACF的get_field函数在这里非常适用于返回中继器值数组


希望这有帮助

@Myke:你帮了我很大的忙。以下是我谦虚的补充:

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);
}

我不确定您是否仍然对解决方案感兴趣,但我能够修改json api插件models/post.php文件,以将转发器数据显示为数组。这是对由所做修改的修改

将set_custom_fields_value()函数替换为以下函数:

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);

    }
}

当前版本的ACF在调用JSON API时打印出一个自定义的_fields对象,其中包含与帖子或页面相关的所有字段。我编辑了@Myke version,将ACF选项页面中的特定自定义字段添加到每个帖子或页面。不幸的是,整个选项页没有get_fields()函数,因此您必须根据字段结构对其进行编辑

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
    }
    else if (isset($response['page'])) {
        json_api_add_acf($response['page']); // Add a specs to a page
    }

    return $response;
}

function json_api_add_acf(&$post) {
    $post->custom_fields->NAME_OF_YOUR_CUSTOM_FIELD = get_field( 'NAME_OF_YOUR_CUSTOM_FIELD', 'option' );
}

现在有一个小插件为您添加过滤器


Wordpress 4.7的更新

随着WordPress4.7的发布,REST功能不再作为一个独立的插件提供,而是以滚动方式提供(不需要插件)

以前的过滤器似乎不起作用。但是,以下代码段确实存在(可以在
functions.php
中):

=PHP 5.3

add_filter('rest_prepare_post', function($response) {
    $response->data['acf'] = get_fields($response->data['id']);
    return $response;
});
add_filter('rest_prepare_post', 'append_acf');

function append_acf($response) {
    $response->data['acf'] = get_fields($response->data['id']);
    return $response;
};

add_filter('rest_prepare_post', function($response) {
    $response->data['acf'] = get_fields($response->data['id']);
    return $response;
});
add_filter('rest_prepare_post', 'append_acf');

function append_acf($response) {
    $response->data['acf'] = get_fields($response->data['id']);
    return $response;
};
注意:过滤器是一个通配符过滤器,应用如下

apply_filters("rest_prepare_$type", ...
因此,如果您有多种内容类型(自定义),则需要执行以下操作:

add_filter('rest_prepare_multiple_choice', 'append_acf');
add_filter('rest_prepare_vocabularies', 'append_acf');

function append_acf($response) {
    $response->data['acf'] = get_fields($response->data['id']);
    return $response;
};

注意似乎每个记录都调用了
rest\u prepare\u x
。因此,如果你ping索引端点,它将被调用多次(因此你不需要检查它的帖子或帖子)

我有相同的ipad magazing项目,我的网站管理员是wordpress,你能告诉我你是如何管理这个ACF输出到JSON的吗?…很好的例子!非常感谢。您是否对JSON API做了更多的修改?关于自定义字段/post meta?LIFESAVER的更多提示!我花了一点时间才找到它的位置,我必须阅读几乎所有JSON的API for WP文档。(jsonapi.php)非常感谢!它就像一个符咒-谢谢你的兄弟