This is a starter template for building a Next.js application that fetches data from a WordPress site using the WordPress REST API. The template includes functions for fetching posts, categories, tags, authors, and featured media from a WordPress site and rendering them in a Next.js application.
next-wp
is built with Next.js 14, React, Typescript, Tailwind, shadcn/ui, and brijr/craft. It pairs nicely with brijr/components for a rapid development experience. Built by 9d8.
lib/wordpress.ts
-> Functions for fetching WordPress CMS via Rest APIlib/wordpress.d.ts
-> Type declarations for WP Rest APIcomponents/posts/post-card.tsx
-> Component and styling for postsapp/posts/filter.tsx
-> Component for handling filtering of posts
There are two env
variables are required to be set in .env.local
file:
WORDPRESS_URL="https://wordpress.com"
WORDPRESS_HOSTNAME="wordpress.com"
You can find the example of .env.local
file in the .env.example
file (and in Vercel):
The lib/wordpress.ts
file contains several functions for fetching data from a WordPress site using the WordPress REST API. Here's a brief overview of each function:
-
getAllPosts(filterParams?: { author?: string; tag?: string; category?: string; })
: Fetches all posts from the WordPress site. Optionally, you can pass filter parameters to filter posts by author, tag, or category. -
getPostById(id: number)
: Fetches a single post by its ID. -
getPostBySlug(slug: string)
: Fetches a single post by its slug. -
getAllCategories()
: Fetches all categories from the WordPress site. -
getCategoryById(id: number)
: Fetches a single category by its ID. -
getCategoryBySlug(slug: string)
: Fetches a single category by its slug. -
getPostsByCategory(categoryId: number)
: Fetches all posts belonging to a specific category by its ID. -
getPostsByTag(tagId: number)
: Fetches all posts tagged with a specific tag by its ID. -
getTagsByPost(postId: number)
: Fetches all tags associated with a specific post by its ID. -
getAllTags()
: Fetches all tags from the WordPress site. -
getTagById(id: number)
: Fetches a single tag by its ID. -
getTagBySlug(slug: string)
: Fetches a single tag by its slug. -
getAllPages()
: Fetches all pages from the WordPress site. -
getPageById(id: number)
: Fetches a single page by its ID. -
getPageBySlug(slug: string)
: Fetches a single page by its slug. -
getAllAuthors()
: Fetches all authors from the WordPress site. -
getAuthorById(id: number)
: Fetches a single author by their ID. -
getAuthorBySlug(slug: string)
: Fetches a single author by their slug. -
getPostsByAuthor(authorId: number)
: Fetches all posts written by a specific author by their ID. -
getPostsByAuthorSlug(authorSlug: string)
: Fetches all posts written by a specific author by their slug. -
getPostsByCategorySlug(categorySlug: string)
: Fetches all posts belonging to a specific category by its slug. -
getPostsByTagSlug(tagSlug: string)
: Fetches all posts tagged with a specific tag by its slug. -
getFeaturedMediaById(id: number)
: Fetches the featured media (e.g., featured image) by its ID.
These functions provide a convenient way to interact with the WordPress REST API and retrieve various types of data from your WordPress site. They can be used in your Next.js application to fetch and display WordPress content.
The lib/wordpress.d.ts
file contains TypeScript type definitions for various WordPress entities and related data structures. Here's an overview of the main types:
-
Post
: Represents a WordPress post with properties such asid
,title
,content
,excerpt
,author
,categories
,tags
, and more. -
Category
: Represents a WordPress category with properties likeid
,name
,slug
,description
,parent
, andcount
. -
Tag
: Represents a WordPress tag with properties similar toCategory
, includingid
,name
,slug
,description
, andcount
. -
Page
: Represents a WordPress page with properties likeid
,title
,content
,excerpt
,author
,parent
, andtemplate
. -
Author
: Represents a WordPress author with properties such asid
,name
,slug
,description
,avatar_urls
, andmeta
. -
BlockType
: Represents a WordPress block type with properties likename
,title
,description
,icon
,category
,attributes
, and more. -
EditorBlock
: Represents a block in the WordPress editor with properties likeid
,name
,attributes
,innerBlocks
, andinnerHTML
. -
TemplatePart
: Represents a template part in WordPress with properties such asid
,slug
,theme
,type
,content
,title
, andstatus
. -
SearchResult
: Represents a search result from WordPress with properties likeid
,title
,url
,type
, andsubtype
. -
FeaturedMedia
: Represents featured media (e.g., featured image) in WordPress with properties likeid
,title
,caption
,alt_text
,media_details
, andsource_url
. -
FilterBarProps
: Represents the props for a filter bar component with propertiesauthors
,tags
,categories
, and selected values for each.
These type definitions provide type safety and autocompletion when working with WordPress data in your Next.js application. They ensure that you are accessing the correct properties and passing the expected data types when interacting with the WordPress REST API.
The components/posts/post-card.tsx
file contains the PostCard
component, which is responsible for rendering a single post card in the application. Here's an overview of the component:
post
: APost
object representing the WordPress post to be rendered.
-
The component fetches the featured media, author, and category associated with the post using the
getFeaturedMediaById
,getAuthorById
, andgetCategoryById
functions fromlib/wordpress.ts
. -
It formats the post date using the
toLocaleDateString
method with the specified options. -
The component renders a link to the individual post page using the post's slug.
-
Inside the link, it displays the post's featured image, title, excerpt, category, and date.
-
The post title and excerpt are rendered using the
dangerouslySetInnerHTML
attribute to handle HTML content. -
The component applies various CSS classes to style the post card, including hover effects and transitions.
To use the PostCard
component, import it into your desired page or component and pass a Post
object as the post
prop.
The components/posts/filter.tsx
file contains the FilterPosts
component, which provides a filtering interface for posts based on tags, categories, and authors. Here's an overview of the component:
authors
: An array ofAuthor
objects representing the available authors to filter by.tags
: An array ofTag
objects representing the available tags to filter by.categories
: An array ofCategory
objects representing the available categories to filter by.selectedAuthor
: An optional string representing the currently selected author ID.selectedTag
: An optional string representing the currently selected tag ID.selectedCategory
: An optional string representing the currently selected category ID.
-
The component uses the
useRouter
hook from Next.js to handle navigation and URL updates based on the selected filters. -
It renders three
Select
components for filtering posts by tag, category, and author. EachSelect
component displays the available options and allows the user to select a specific value or choose "All" to reset the filter. -
When a filter value is changed, the
handleFilterChange
function is called with the filter type and selected value. It updates the URL query parameters accordingly and navigates to the updated URL. -
The component also includes a "Reset Filters" button that, when clicked, calls the
handleResetFilters
function to navigate back to the/posts
page without any filters applied. -
The selected filter values are passed as props to the component and used to set the initial values of the
Select
components. -
The selected filter values are passed as props to the component and used to set the initial values of the
Select
components. -
The selected filter values are passed as props to the component and used to set the initial values of the
Select
components.
Built by Bridger Tower and Cameron Youngblood at 9d8