我正在開發一本雜誌的WordPress網站,將擁有移動應用中一個JSON飼料。我使用高級自定義字段和每個文章中的多個文章和多個頁面的Repeater字段來設置後端。 http://www.advancedcustomfields.com/add-ons/repeater-field/ JSON API來顯示高級自定義字段 - WordPress的
我正在使用JSON API,但是這不包括我的任何自定義字段。目前是否有插件可以做到這一點?
我正在開發一本雜誌的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