I try to extract a certain part from an external WordPress page (namely the post) and then embed it in a simple site.
I tried with .load()
but it doesn't seem to work and I don't really know how to proceed.
I try to extract a certain part from an external WordPress page (namely the post) and then embed it in a simple site.
I tried with .load()
but it doesn't seem to work and I don't really know how to proceed.
Wordpress provides REST API for access to its posts, comments, pages, etc. For posts you can look at https://developer.wordpress.org/rest-api/reference/posts/#list-posts entry.
So, you can use next code to extract list of posts:
$.ajax({
method: "GET",
url: "http://your.website/wp-json/wp/v2/posts/",
dataType: "json",
})
.done(function(response){console.log(response);})
.fail(function(xhr, status){console.log(status);});
Remember, you need to have CORS allowed for such kind of manipulations. See how to enable CORS in Wordpress at https://joshpress.net/access-control-headers-for-the-wordpress-rest-api/.
If it is not your website, read about how CORS affects your AJAX requests and what you can do with it at https://stackoverflow.com/a/17299796/2678487.
For example, you can use something like next to access Wordpress REST API on my website:
$.ajax({
method: "GET",
url: "http://cors-anywhere.herokuapp.com/blog.binaryspaceship.com/wp-json/wp/v2/posts/",
dataType: "json",
})
.done(function(response){
$('#result').html(
JSON.stringify(response, null, 2)
);
})
.fail(function(xhr, status){alert(status);});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<pre id="result"></pre>