Wp Rest Api 自定义端点 [英] Wp Rest Api Custom End point

查看:45
本文介绍了Wp Rest Api 自定义端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将自定义端点添加到我的 wp-rest api 最新版本.我已经有了这个,但最后带有 slug 参数的那个不起作用..有谁知道为什么..如果有人能帮忙的话会很棒..

Im trying to add a custom end point to my wp-rest api the latest version. I have this already but the one with the slug param at the end does not work.. Does any one know why.. would be great if anyone could help..

     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'
    ) );

推荐答案

我猜是因为你使用了 d 元字符作为正则表达式 (?P<slug>\d+)这是数字的意思,请尝试使用 S 代替.代码应该是这样的

i guess it because you used d metacharacter for regex (?P<slug>\d+) that's mean for digit, please try use S instead. The code should look like this

register_rest_route( 'wp/v2', '/guestmix/(?P<slug>\S+)', array(
    'methods' => 'GET',
    'callback' => 'get_guestmix'
) );

这是供参考的备忘单http://www.phpliveregex.com/

这篇关于Wp Rest Api 自定义端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆