我正在尝试将自定义端点添加到最新版本的wp-rest api。我已经有这个了,但是最后带有子弹参数的那个不起作用..有谁知道为什么..如果有人可以帮助,那将会很棒。
register_rest_route( 'wp/v2', '/guestmix', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_guestmixes' )
),
'schema' => array( $this, 'get_public_item_schema' )
) );
register_rest_route( 'wp/v2', '/guestmix/(?P<slug>\d+)', array(
'methods' => 'GET',
'callback' => 'get_guestmix'
) );
Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用 WordPress REST API 并将 GET 请求发送到:
http://myblog/wp-json/wp/v2/posts
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但我想指定字段,尽管我不知道如何。我已经查看了文档,但仍然不知道如何去做。例如,使用公共 API:
https://public-api.wordpress.com/rest/v1.1/sites/www.mysite.com/posts?number=100&fields=title,excerpt,featured_image
Run Code Online (Sandbox Code Playgroud)
只返回指定的字段。我如何使用 v2 API 做到这一点?
我有一个自定义帖子类型“产品”,我使用高级自定义字段(ACF) 插件为其创建了自定义字段。
\n\n当我请求每个产品都有acf 属性的产品时,例如:/wp-json/wp/v2/products
"acf": {\n "rating": 77\n "fun-factor": 10,\n "something": 65,\n \xe2\x80\xa6\n}\nRun Code Online (Sandbox Code Playgroud)\n\n现在,我products按我的属性进行过滤/wp-json/wp/v2/products?filter[meta_key]=rating&filter[meta_value]=77,并将结果返回到上面的产品。到目前为止,一切都很好。
现在我有两件事想要完成:
\n\n我很高兴收到任何意见!
\n我已经在我的 WordPress 网站上安装了 wp-json-rest-api,并且我想将我的网站连接到 Android 应用程序。在这样的 url 中,它会获取所有帖子:
http://mywebsite.com/wp-json/posts
Run Code Online (Sandbox Code Playgroud)
在 Android 页面之一上,我只想获取帖子标题,仅此而已。我的意思是我应该如何在 url 或其他任何地方指定我只想要标题和 ID 而不是帖子的所有内容?例如,在每个帖子的页面中,我只想获取该帖子的标题、id 和内容,如下所示,其中帖子的 id 为 12:
http://mywebsite.com/wp-json/posts?include=title,id,content/12
Run Code Online (Sandbox Code Playgroud)
但在其他页面(例如所有帖子列表)中,我想要这样的内容:
http://mywebsite.com/wp-json/posts?include=title,id
Run Code Online (Sandbox Code Playgroud)
我应该怎么办?
我正在尝试为WordPress REST API创建自定义端点,并通过URL传递参数。
当前端点是:
/wp-json/v1/products/81838240219
我想要实现的是一个看起来像这样的端点,并且能够在回调中检索identifier参数。
/wp-json/v1/products?identifier=81838240219
// Custom api endpoint test
function my_awesome_func( $data ) {
$identifier = get_query_var( 'identifier' );
return $identifier;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'api/v1', '/products=(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );
Run Code Online (Sandbox Code Playgroud) 我有一个 WordPress 后端,我在其中向 API 添加了自己的自定义端点:
// retrieve countries
register_rest_route( $namespace, '/countries',
array(
'methods' => 'GET',
'callback' => array( $this, 'get_countries' ),
)
);
// check answer
register_rest_route( $namespace, '/check_answer',
array(
'methods' => 'POST',
'callback' => array( $this, 'check_answer' ),
)
);
Run Code Online (Sandbox Code Playgroud)
我已经这样设置了我的环境:https : //example.com是 React 应用程序所在的位置,WordPress 位于https://example.com/wp上的子目录中
我的 React 应用程序向上述端点发出 POST 和 GET 请求。我有一个生产环境变量,我在其中设置了 API 的基本 URL,即https://example.com/wp/wp-json/game(“游戏”是我的命名空间),因此我可以使用 Axios 向https://example.com/wp/wp-json/game/countries和发出请求,https://example.com/wp/wp-json/game/check_answer问题就来了。
我的服务器已配置为可以在不使用www. 所以https://example.com和https://www.example.com都服务于同一个应用程序。
但这会为我的自定义端点带来一些有趣的问题:GET 请求始终有效。但 POST …
我有一个 WordPress 网站,我用它来填充博客文章和自定义文章类型下的一些私人文章。在另一个网站上,我使用 REST API 来显示帖子。如果我使用像 Postman 这样的软件,我可以显示来自 REST API 的数据。
如何防止对域 www.example.com 的任何未经授权的 REST API 请求?因此,如果请求不是来自 www.mysite.com,它会被阻止吗?
如果我的自定义帖子类型(example.com)不是来自 mysite.com,则基本上防止它对其余 api 可见
We are trying to issue http requests from our ionic app to a Wordpress server (REST API) and get the following error:
{"_body":{"isTrusted":true},"status":0,"ok":false,"statusText":"","headers":{},"type":3,"url":null}.
The requests are working fine when we use the web view or ionic DevApp and are failing on iOS devices.
What we did so far:
这是西奇:
我下载并安装了 Wampserver64 和 Wordpress 5.2.3
我终于到达了我的网站,但我无法使用新的古腾堡或块编辑器预览或发布页面,因为某些内容已损坏!当我使用经典编辑器进行编辑时,一切都很好。
****这不是我从站点健康插件收到的通知****
REST API 是 WordPress 和其他应用程序与服务器通信的一种方式。一个例子是块编辑器屏幕,它依赖于此来显示和保存您的帖子和页面。REST API 调用给出以下意外结果: (404) Not Found 在此服务器上找不到请求的 URL /wordpress/wp-json/wp/v2/types/post。Apache/2.4.39 (Win64) PHP/7.3.5 服务器位于站点文件夹端口 80
我已经在互联网上搜索了如何解决这个问题,但到目前为止还没有。非常感谢帮助!
wordpress json wampserver wordpress-rest-api wordpress-gutenberg