wordpress 自定义_为WordPress REST API创建自定义端点

wordpress 自定义

This WordPress REST API tutorial walks you through creating a custom WP-API endpoint. We’ll first create a child theme of the default “Twenty Seventeen” theme, which will allow us to add functionality to our theme, and then proceed to register our custom API endpoint.

本WordPress REST API教程将引导您创建自定义WP-API端点。 我们将首先创建默认的“二十七岁”主题的子主题,这将使我们能够向主题添加功能,然后继续注册我们的自定义API端点。

The WordPress REST API provides you with more than just a set of built-in routes. You can also create custom routes and endpoints using the same APIs used to create default routes (for example, the register_rest_route() function and the WP_Rest_Controller class etc.). With WP-API, you’ll have the possibility to integrate WordPress with other ecosystems, which makes WordPress a powerful and modern application development platform.

WordPress REST API为您提供的不仅仅是一组内置路由。 您还可以使用用于创建默认路由的相同API(例如register_rest_route()函数和WP_Rest_Controller类等)来创建自定义路由和端点。 使用WP-API,您将可以将WordPress与其他生态系统集成,这使WordPress成为功能强大且现代的应用程序开发平台。

You can create or register custom endpoints either in plugins or themes.

您可以在插件或主题中创建或注册自定义端点。

创建儿童主题 (Creating a Child Theme)

Inside your WordPress installation folder, create a folder for your child theme. Let’s call it twentyseventeen-child:

在您的WordPress安装文件夹中,为您的子主题创建一个文件夹。 我们称它为twentyseventeen-child

cd /var/www/html/wp-content/themes
mkdir twentyseventeen-child
Creating the child theme folder

Next create a style.css file:

接下来创建一个style.css文件:

touch style.css

And add the following header information:

并添加以下标头信息:

/*
 Theme Name:  Twenty Seventeen Child Theme
 description: A child theme of the Twenty Seventeen WordPress theme
 Author:       Ahmed Bouchefra
 Template:     twentyseventeen
 Version:      1.0.0
*/

The Template field refers to the folder’s name of the parent theme.

模板字段是指父主题的文件夹名称。

Go to Appearance -> Themes in the WordPress admin and choose your child theme:

转到WordPress管理员中的外观->主题 ,然后选择您的子主题:

Choosing your WordPress child theme in the WordPress themes section

Next, click on the Activate button to activate your child theme:

接下来,单击“ 激活”按钮以激活您的子主题:

The Activate button

Inside the theme folder, add a functions.php file with the following initial code:

在主题文件夹内,添加带有以下初始代码的functions.php文件:

<?php
// Add code here.

创建自定义WP-API端点 (Creating a Custom WP-API Endpoint)

We want to create a new route that will allow us to retrieve the latest recent posts by category ID with the following format:

我们想创建一条新路线,使我们可以按以下格式按类别ID检索最近的最新帖子:

http://localhost/wp-json/mytwentyseventeentheme/v1/latest-posts/<CATEGORY_ID>

At this point, if we visit the above URL in our browser we’ll get a 404 error with the message “No route was found matching the URL and request method”:

此时,如果我们在浏览器中访问上述URL,将收到404错误,并显示消息“找不到与URL和请求方法匹配的路由”

404 error message

This is because we don’t actually have that route. Let’s change that!

这是因为我们实际上没有那条路线。 让我们改变它!

In the functions.php file of your theme, add the following code:

在主题的functions.php文件中,添加以下代码:

add_action('rest_api_init', function () {
  register_rest_route( 'mytwentyseventeentheme/v1', 'latest-posts/(?P<category_id>\d+)',array(
                'methods'  => 'GET',
                'callback
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值