Built with axios, this package provides a wrapper for Wordpress REST API for fetching data from wordpress hosted sites.
$ npm install wordpress-sdk --save
or if you use yarn
$ yarn add wordpress-sdk
import { wordpress } from 'wordpress-sdk'
// or
const { wordpress } = require('wordpress-sdk')
wordpress.initialize({
// your wordpress rest api base URL
url: 'https://developer.wordpress.org/wp-json/wp/v2',
})
wordpress
.allPosts()
.then(response => {
console.log(response.posts)
})
.catch(error => {
console.log(error.message)
})
Or using async await
try {
const { posts } = await wordpress.allPosts()
console.log(posts)
} catch (error) {
console.log(error.message)
}
wordpress
.allCategories()
.then(response => {
console.log(response.categories)
})
.catch(error => {
console.log(error.message)
})
Or using async await
try {
const { categories } = await wordpress.allCategories()
console.log(categories)
} catch (error) {
console.log(error.message)
}