jquery - 如何使用 WordPress REST API 获取单个自定义 Post-Type 项目
问题描述
我有一个名为的自定义帖子类型product
,它有一个带有标题、没有帖子内容和一堆自定义元的帖子。我下面的 jQuery 代码返回 404 错误。到目前为止,我还没有在 WP Rest API 文档中找到有关获取单个自定义帖子类型帖子的任何内容,尽管我确实看到您可以使用post
帖子类型这样做。我正在尝试做的事情还可能吗?
jQuery(document).ready(function($){
$( '#product-select' ).on( 'change', function(){ //changing a drop-down triggers the function
var productID = $(this).val();
$.ajax({
type: 'GET',
url: '/wp-json/wp/v2/types/product/137', // <= /product works but /product/137 does not
success: function( data ){
console.log( data );
}
});
});
});
解决方案
URL 是/wp-json/wp/v2/product
- 删除“类型”路径。show_in_rest
并在注册您的cpt 时检查您是否已设置为true。
这也是一个非常常见的用例,因此谷歌搜索“wordpress rest api custom post type”会很容易地向您展示这一点。
推荐阅读
- haskell - 将 GTK3 小部件颜色设置为计算值的正确方法
- aem - CRXDE 不允许我上传/下载包
- mysql - 如何通过命令行比较两个 MARIADB 模式
- rust - 使用实现 `tokio::io::AsyncWrite` 的动态类型对象
- drake - 有没有办法将上下文中不同大小的向量存储为离散状态
- python - django 建立模型之间的正确关系
- javascript - 如果金额达到一定值,则删除运费
- ios - 不断收到错误“预计解码数组
但找到了字典| 迅速 - c++ - 严重包含在字符串中,除非我将它存储在 C++ 类中
- c++ - 函数模板和“正常”函数之间奇怪的不一致