2017-05-15 45 views
1

我想在不使用任何插件的情況下執行此操作,因爲這些插件都是核心WordPress功能(自定義字段和REST API)。下面是自定義字段的文檔以供參考:如何將WordPress中定義的自定義字段添加到wordpress的其他API響應中

https://codex.wordpress.org/Using_Custom_Fields

這裏是我的WordPress安裝截圖:

wordpress custom fields

這裏是一個職位API響應看起來像目前:

{ 
    "_links": { 
     "about": [ 
      { 
       "href": "http://example.com/wp-json/wp/v2/types/post" 
      } 
     ], 
     "author": [ 
      { 
       "embeddable": true, 
       "href": "http://example.com/wp-json/wp/v2/users/1" 
      } 
     ], 
     "collection": [ 
      { 
       "href": "http://example.com/wp-json/wp/v2/posts" 
      } 
     ], 
     "curies": [ 
      { 
       "href": "https://api.w.org/{rel}", 
       "name": "wp", 
       "templated": true 
      } 
     ], 
     "replies": [ 
      { 
       "embeddable": true, 
       "href": "http://example.com/wp-json/wp/v2/comments?post=21" 
      } 
     ], 
     "self": [ 
      { 
       "href": "http://example.com/wp-json/wp/v2/posts/21" 
      } 
     ], 
     "version-history": [ 
      { 
       "href": "http://example.com/wp-json/wp/v2/posts/21/revisions" 
      } 
     ], 
     "wp:attachment": [ 
      { 
       "href": "http://example.com/wp-json/wp/v2/media?parent=21" 
      } 
     ], 
     "wp:featuredmedia": [ 
      { 
       "embeddable": true, 
       "href": "http://example.com/wp-json/wp/v2/media/23" 
      } 
     ], 
     "wp:term": [ 
      { 
       "embeddable": true, 
       "href": "http://example.com/wp-json/wp/v2/categories?post=21", 
       "taxonomy": "category" 
      }, 
      { 
       "embeddable": true, 
       "href": "http://example.com/wp-json/wp/v2/tags?post=21", 
       "taxonomy": "post_tag" 
      } 
     ] 
    }, 
    "author": 1, 
    "categories": [ 
     5, 
     4 
    ], 
    "comment_status": "open", 
    "content": { 
     "protected": false, 
     "rendered": "" 
    }, 
    "date": "2017-05-14T15:25:33", 
    "date_gmt": "2017-05-14T15:25:33", 
    "excerpt": { 
     "protected": false, 
     "rendered": "" 
    }, 
    "featured_media": 23, 
    "format": "standard", 
    "guid": { 
     "rendered": "http://example.com/?p=21" 
    }, 
    "id": 21, 
    "link": "http://example.com/2017/05/14/post/", 
    "meta": [], 
    "modified": "2017-05-15T18:17:34", 
    "modified_gmt": "2017-05-15T18:17:34", 
    "ping_status": "open", 
    "slug": "", 
    "sticky": false, 
    "tags": [], 
    "template": "", 
    "title": { 
     "rendered": "" 
    }, 
    "type": "post" 
} 

萬一它可能是相關的,這裏是我的活動插件:

list of active wordpress plugins: Enable Media Replace, S3 Uploads

任何幫助是極大的讚賞。謝謝!

回答

2

首先,你需要register_rest_fields在WP REST API JSON響應添加自定義端點

add_action('rest_api_init', 'add_custom_fields'); 
function add_custom_fields() { 
register_rest_field(
'post', 
'custom_fields', //New Field Name in JSON RESPONSEs 
array(
    'get_callback' => 'get_custom_fields', // custom function name 
    'update_callback' => null, 
    'schema'   => null, 
    ) 
); 
} 

然後測試在本地站點

enter image description here

定義功能 get custom fields

function get_custom_fields($object, $field_name, $request) { 
//your code goes here 
return $customfieldvalue; 
} 

+0

謝謝,這是有道理的。這段代碼應該去哪裏? – quinn

+0

在您的主題功能中,或者如果您在插件功能中使用插件 –

相關問題