Content Loaders
dewp/loaders loads your content using the WordPress REST API so you can use it with Astro’s
content collection API.
This project contains several tools for using your WordPress data in Astro projects. You can use them to build headless WordPress sites with Astro or even migrate your data away from WordPress to Astro entirely.
Content Loaders
dewp/loaders loads your content using the WordPress REST API so you can use it with Astro’s
content collection API.
Starter Template
Use DeWP’s blog template to get started with an Astro project using headless WordPress data.
More coming soon!
The easiest way to get started is to create a new Astro project using the DeWP blog template.
Run the following command in your terminal and follow the instructions in the Astro install wizard.
npm create astro -- --template delucis/dewp/examples/blog-starteryarn create astro --template delucis/dewp/examples/blog-starterpnpm create astro --template delucis/dewp/examples/blog-starterUpdate the endpoint option in src/content/config.ts to the URL of your WordPress REST API. Most commonly this is the URL of your website with /wp-json/ at the end.
import { wpCollections } from 'dewp/loaders';
export const collections = wpCollections({ endpoint: 'https://example.com/wp-json/',});You can now start the Astro dev server and see a live preview of your project while you build.
The template shows links to all the pages in the linked WordPress site on the homepage and links to the 10 most recent blog posts at /blog.
If you prefer to add the DeWP content loaders to an existing Astro project, follow these steps.
Install DeWP by running the following command in your project directory.
npm i dewpyarn add dewppnpm add dewpAdd the DeWP content loaders to src/content/config.ts, setting endpoint to the URL of your WordPress REST API.
import { z, defineCollection } from 'astro:content';import { wpCollections } from 'dewp/loaders';
export const collections = { // existing collections ... ...wpCollections({ endpoint: 'https://example.com/wp-json/' }),};You can now use Astro’s content collection APIs to get your WordPress data in your components.
In the following example, the posts collection is loaded in order to display each post’s title:
---import { getCollection } from 'astro:content';const posts = await getCollection('posts');---{posts.map((post) => <p>{post.data.title}</p>)}