########## Creating a Child Theme ############
cd /var/www/html/wp-content/themes
mkdir twentyseventeen-child
Go to Appearance -> Themes in the WordPress admin and choose your child theme:
Inside the theme folder, add a functions.php file with the following initial code:
In the functions.php file of your theme, add the following code:
add_action('rest_api_init', function () {
register_rest_route( 'mytwentyseventeentheme/v1', 'latest-posts/(?P<category_id>\d+)',array(
'methods' => 'GET',
'callback' => 'get_latest_posts_by_category'
));
});
We’re using the register_rest_route() with the following parameters:
a namespace, mytwentyseventeentheme/v1
a resource path with a regex for catching the category ID, latest-posts/(?P<category_id>\d+)
an option array where we specify the GET method and a get_latest_posts_by_category() callback function that handles the request.
########## Creating a route ############
We want to create a new route that will allow us to retrieve the latest recent posts by category ID with the following format:
http://localhost/wp-json/mytwentyseventeentheme/v1/latest-posts/<CATEGORY_ID>
> Implementing the Callback Function
get_latest_posts_by_category() function as the callback that will be called for processing and handling the GET request, let’s actually implement it:
function get_latest_posts_by_category($request) {
$args = array(
'category' => $request['category_id']
);
$posts = get_posts($args);
if (empty($posts)) {
return new WP_Error( 'empty_category', 'there is no post in this category', array('status' => 404) );
}
$response = new WP_REST_Response($posts);
$response->set_status(200);
return $response;
}
> We first retrieve the category_id argument from the $request parameter by direct access. Next we create an $args array with the category key set to the value of category_id that will be extracted from the route.
**** function.php ****
add_action('rest_api_init', function () {
$latest_posts_controller = new Latest_Posts_Controller();
$latest_posts_controller->register_routes();
$latest_posts_controller->my_routes();
}
class Latest_Posts_Controller extends WP_REST_Controller {
public function register_routes() {
$namespace = 'mytwentyseventeentheme/v1';
$path = 'latest-posts/(?P<category_id>\d+)';
register_rest_route( $namespace, '/' . $path, [
array(
'methods' => 'GET',
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' )
),
]);
}
public function get_items($request) {
$args = array(
'category' => $request['category_id']
);
$posts = get_posts($args);
if(empty($posts)) {
return new WP_Error( 'empty_category', 'there is no post in this category', array( 'status' => 404 ) );
}
return new WP_REST_Response($posts, 200);
}
}
cd /var/www/html/wp-content/themes
mkdir twentyseventeen-child
Go to Appearance -> Themes in the WordPress admin and choose your child theme:
Inside the theme folder, add a functions.php file with the following initial code:
In the functions.php file of your theme, add the following code:
add_action('rest_api_init', function () {
register_rest_route( 'mytwentyseventeentheme/v1', 'latest-posts/(?P<category_id>\d+)',array(
'methods' => 'GET',
'callback' => 'get_latest_posts_by_category'
));
});
We’re using the register_rest_route() with the following parameters:
a namespace, mytwentyseventeentheme/v1
a resource path with a regex for catching the category ID, latest-posts/(?P<category_id>\d+)
an option array where we specify the GET method and a get_latest_posts_by_category() callback function that handles the request.
########## Creating a route ############
We want to create a new route that will allow us to retrieve the latest recent posts by category ID with the following format:
http://localhost/wp-json/mytwentyseventeentheme/v1/latest-posts/<CATEGORY_ID>
> Implementing the Callback Function
get_latest_posts_by_category() function as the callback that will be called for processing and handling the GET request, let’s actually implement it:
function get_latest_posts_by_category($request) {
$args = array(
'category' => $request['category_id']
);
$posts = get_posts($args);
if (empty($posts)) {
return new WP_Error( 'empty_category', 'there is no post in this category', array('status' => 404) );
}
$response = new WP_REST_Response($posts);
$response->set_status(200);
return $response;
}
> We first retrieve the category_id argument from the $request parameter by direct access. Next we create an $args array with the category key set to the value of category_id that will be extracted from the route.
**** function.php ****
add_action('rest_api_init', function () {
$latest_posts_controller = new Latest_Posts_Controller();
$latest_posts_controller->register_routes();
$latest_posts_controller->my_routes();
}
class Latest_Posts_Controller extends WP_REST_Controller {
public function register_routes() {
$namespace = 'mytwentyseventeentheme/v1';
$path = 'latest-posts/(?P<category_id>\d+)';
register_rest_route( $namespace, '/' . $path, [
array(
'methods' => 'GET',
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' )
),
]);
}
public function get_items($request) {
$args = array(
'category' => $request['category_id']
);
$posts = get_posts($args);
if(empty($posts)) {
return new WP_Error( 'empty_category', 'there is no post in this category', array( 'status' => 404 ) );
}
return new WP_REST_Response($posts, 200);
}
}
Comments
Post a Comment