1
我剛剛開始使用REST API並使用它從前端創建帖子。我設法發佈標題,摘錄,內容的帖子。 我想添加一個自定義元字段值,以及任何示例或幫助,非常感謝。使用Meta字段創建帖子 - WP REST API
這是我的Ajax代碼,是不是在後期添加的所有其他領域除了meta值做工精細
jQuery(document).ready(function ($) {
$('#post-submission-form').on('submit', function(e) {
e.preventDefault();
var title = $('#post-submission-title').val();
var excerpt = $('#post-submission-excerpt').val();
var content = $('#post-submission-content').val();
var status = 'draft';
var data = {
title: title,
excerpt: excerpt,
content: content,
status: status,
meta: {
'video_url_url' : 'abc',
}
};
$.ajax({
method: "POST",
url: POST_SUBMITTER.root + 'wp/v2/posts',
data: data,
beforeSend: function (xhr) {
xhr.setRequestHeader('X-WP-Nonce', POST_SUBMITTER.nonce);
},
success : function(response) {
console.log(response);
alert(POST_SUBMITTER.success);
},
fail : function(response) {
console.log(response);
alert(POST_SUBMITTER.failure);
}
});
});
});
解決了它。我必須先註冊meta字段才能讀取或寫入。 '$ object_type ='post'; $ args1 = array(//驗證並清理元值 //'number'必須用作'type' 'type'=>'string', //顯示元鍵的模式 'description'=>'一個與字符串元值相關聯的元密鑰', //返回一個單一的值類型 'single'=> true, //在WP REST API響應中顯示。 'show_in_rest'=> true, ); register_meta($ object_type,'video_url_url',$ args1);' –
我試圖做同樣的事情。我仍然無法工作。你可以看看我的問題:http://stackoverflow.com/q/43265580/100747 – input