【问题标题】:JSON API to show Advanced Custom Fields - WordPress用于显示高级自定义字段的 JSON API - WordPress
【发布时间】:2012-04-25 08:05:01
【问题描述】:

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

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

【问题讨论】:

  • 我有同样的 ipad magazing 项目,我的 WebAdmin 是 wordpress,你能告诉我你是如何管理这个 ACF 输出到 JSON 的吗?...

标签: json wordpress


【解决方案1】:

@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 for WP 文档。 (json-api.php)
【解决方案2】:

Wordpress 4.7

的更新

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

以前的过滤器似乎不起作用。但是,以下 sn-p 可以(可以在您的 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;
};

注意过滤器是一个通配符过滤器,应用类似于

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

【讨论】:

    【解决方案3】:

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

    我的嵌套级别比您少一个,因此这可能需要稍作修改。但是 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 函数在这里可以完美地恢复中继器值的数组。

    希望这会有所帮助!

    【讨论】:

    • 很好的例子!非常感谢。您是否需要更改以使用 JSON API 做更多的工作?还有更多关于自定义字段/帖子元的提示吗?
    【解决方案4】:

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

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

    【讨论】:

    • 非常感谢!它就像一个魅力! :-D
    • 谢谢兄弟
    【解决方案5】:

    我不确定您是否仍然对解决方案感兴趣,但我能够修改 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);
    
        }
    }
    

    【讨论】:

      【解决方案6】:

      当前版本的 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' );
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-25
        • 2016-08-12
        • 2012-08-10
        • 1970-01-01
        • 2016-11-11
        • 2018-08-26
        • 2017-08-28
        • 2015-01-21
        相关资源
        最近更新 更多