15

我正在开发一个杂志 WordPress 网站,该网站将为移动应用提供 json 提要。我使用高级自定义字段和多篇文章的重复字段和每篇文章中的多页设置后端。http://www.advancedcustomfields.com/add-ons/repeater-field/ 中继器字段的屏幕截图

我正在使用 JSON API,但这不包括我的任何自定义字段。目前有没有可以做到这一点的插件?

这些是先前字段的自定义字段“slug 名称”

4

6 回答 6

18

@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);
}
于 2013-01-06T08:12:15.330 回答
9

Wordpress 4.7更新

随着 Wordpress 4.7 的发布,REST 功能不再作为一个独特的插件提供,而是作为一个插件提供(不需要插件)。

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

>= PHP 5.3

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

< PHP 5.3

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_prepare_x调用的。因此,如果您正在 ping 索引端点,它将被多次调用(因此您不需要检查它的帖子或帖子)

于 2016-12-07T16:38:18.877 回答
8

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

我的嵌套级别比你少一个,所以这可能需要稍微改变一下。但是 JSON API 插件有一个名为 json_api_encode 的过滤器。我有一个叫做规格的中继器,看起来像这样。

http://d.pr/i/YMvv

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

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 函数在这里完美地工作,用于带回中继器值的数组。

希望这可以帮助!

于 2012-07-27T04:44:54.527 回答
6

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

https://github.com/PanManAms/WP-JSON-API-ACF

于 2014-10-30T16:56:19.857 回答
3

我不确定您是否仍然对解决方案感兴趣,但我能够修改 json-api 插件 models/post.php 文件以将转发器数据显示为数组。这是对http://wordpress-problem.com/marioario-on-plugin-json-api-fixed-get-all-custom-fields-the-right-way/所做修改的修改

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

    }
}
于 2013-04-25T21:35:47.400 回答
0

当前版本的 ACF 在调用 JSON API 时打印出一个 custom_fields 对象,其中包含与帖子或页面相关的所有字段。我编辑了@Myke 版本,将 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' );
}
于 2014-04-10T09:23:42.553 回答