Wp Rest Api自定义端点

use*_*603 5 wordpress wordpress-rest-api

我正在尝试将自定义端点添加到最新版本的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)

Laf*_*ziq 5

我猜是因为您将d正则表达式(?P<slug>\d+)的元字符用于数字,请尝试使用S。该代码应如下所示

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

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

  • 如果只有Wordpress文档需要说明可以使用哪些元字符。 (3认同)