JSON API来显示高级自定义字段 - WordPress的
我正在开发一本杂志的WordPress网站,将拥有移动应用中一个JSON饲料。我使用高级自定义字段和每个文章中的多个文章和多个页面的Repeater字段来设置后端。 http://www.advancedcustomfields.com/add-ons/repeater-field/ JSON API来显示高级自定义字段 - WordPress的
我正在使用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函数在这里完美地工作,用于恢复中继器值的数组。
希望这会有所帮助!
很好的例子!非常感谢。您是否做过更改以使用JSON API做更多工作?有关自定义字段/后meta的更多提示? – Iladarsda 2012-10-26 08:01:19
@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插件模型/ 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);
}
}
电流ACF的版本打印出custom_fields对象到JSON API的调用,包含所有相对于Post或Page的字段。我编辑了@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');
}
现在有一个小插件,为您添加过滤器。
非常感谢!它像一个魅力一样工作! :-D – specialk1st 2016-12-30 18:48:47
感谢兄弟为此 – Vesper 2017-02-03 16:22:15
更新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指标端点,它会被多次调用(所以你不需要检查它的帖子或帖子)
我有相同的ipad magazing项目和我的WebAdmin是wordpress,你能告诉我如何管理这个ACF输出到JSON?... – Denish 2012-05-18 12:42:59