Use WordPress rest API without additional HTTP requests

The WordPress rest API is a great way to import WordPress content in JSON format and can be used in javascript based interfaces. In general, this means that after the page is loaded, the server’s Ajax request for content will become JSON and be rendered on the page without any content. For the first page request, this is an additional request. As long as there is one on the server, won’t you import the content into the JSON included in the response to the first request? This post describes how to use the controller in the rest API to get a rest API response in an appropriate format without other HTTP requests. Then, for example, using this method to get the first page of a post, and then create a post archive, you can switch to the second page of the post using an Ajax request for that content. The
To demonstrate how to get started, we will show you how to use this method to create a simple twenty seven sub topic. Handles archiving of posts of the default post type. GitHub theme posted. To add support for a single post view or other post types, branch and share the generated content. I use twenty seventeen as the basic theme handlebars Use js as the template system. If desired, you can use the preferred tool. To get started, create a subtopic. Functions In PHP, we need a function to get the formatted post, which is passed to wp\u localize\u script() as a rest API response. To do this, you can create exactly the same class that WordPress uses when responding to an actual WordPress HTTP request. The
Wp\u rest\u request – define the wp\u rest\u post\u controller requesting acquisition – Import post wp\u response – Format response this function generates the request manually and does not require other HTTP requests. It is passed through the post controller to execute the query. Then, the wp\u rest\u restore\u response() function provides an appropriate response object with a get\u data() method that can be used with wp\u localize\u script(). Male 1
));
$items = $controller->get\u items ($request);
Return array(
\
);
}Now you can set the JavaScript file to load and pass the data as follows: The
Male
Male
Male
Male {{title.rendered} male
Male
Male
{{{content.rendered}}
Male
Male
In the last section of rendering posts with JavaScript, we found theme in the directory of sub topics on WordPress. You instruct to load a JavaScript file named JS. Continue the build. There, you will reuse the post that wp\u localize\u script sends to the DOM using the template and place it in the container \main. The
JQuery (function ($){
If (\
Var post =ex\u the. Posts,
Template =handlebars. Compile ($(‘\entry-template’) Html()),
$master = $(\
$ Each (post, function (I, post){
$main Append (template (article));
});
}The
}); Now, when you go to the home page of your site, you will see the post
View I posts. The following is an update script that supports paging: JQuery (function ($){
If (\
Var post =ex\u the. Posts,
Template =handlebars. Compile ($(‘\entry-template’) Html()),
Paginationtemplate = handlebars Compile ($(\
$master = $(\
Page=parseint (ex_the.pages),
Page=parseint (ex_the.page);
$ Each (post, function (I, post){
$main Append (template (article));
});
Paging (page);
Function paging (page){
Var hasmore, hasless;
If (page >1){
Hasless= true;
}The
If (Page > page){
Hasmore= fact;
}The
Var paging={
Hasmore: more,
Hasless: hasless
};
$main Append (paginationtemplate (pagination));
$(‘\nav-next’) On (\
Page ++;
Getposts();
});
$(‘\nav previous’) On (\
Page –;
Getposts();
});
}The
Function getposts(){
$ Get (ex_the.api + ‘? Page=’+ page) Done (function (posts, status, XHR){
Var page=xhr. Getresponseheader (‘x-wp-totalpages’);
$main Empty();
$ Each (post, function (I, post){
$main Append (template (article));
});
Paging (page);
});
}The
}The
}); It’s enough to start doing my own. To do this further, it is recommended that you use the history API to update the URL of the page map. You can also add it to other post types and switch to a single post view to create a fairly tidy sub topic. If you do, vuejs is used. Here, check out my post to learn how to use vuejs with the WordPress rest API. Cleaner, probably with less code. The
The real reason for this article is to demonstrate how to avoid attaching HTTP requests when displaying rest API data. Got a great example of how to create a paged article archive without refreshing pages with a bonus.

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *