Do you know how to create a static website with HTML, CSS, and JavaScript? I believe you have used WordPress once to create your own blog.
You can use WordPress, without knowing how to create your own themes and plugins. There are a lot in the wild. But then it starts to get complicated once you start tinkering on things the way you want it to.
What if there’s a way to turn WordPress into an API or a data source in which you could pull content and display it anywhere?
In this post, we will be setting up WordPress in Headless mode, create a basic HTML/CSS template, and use GatsbyJS to integrate our template with WordPress.
WordPress
I will assume you know how to install WordPress. If not, here is a step by step guide.
To setup WordPress in Headless mode, we will need to install the following plugins:
- WP API Menus - This will expose the Menus as an API. The built-in WordPress API does not expose menus.
- Advanced Custom Fields - We would be able to create custom fields based on rules and be able to access it through an API
- Custom Post Type UI - Sometimes Pages and Posts are not enough, we would like to create custom posts like Products, Movies, etc.
- Postlight Headless Theme - This theme will remove the Webpages on the client-facing frontend and will replace it with JSON.
Running in Headless mode means we will still be using the WordPress Dashboard to manage content but we will be serving those through an API. Hence, we won’t be using WordPress to show the generated content to our visitors.
Make sure you have at least 1 post and 1 page on your WordPress site. Make sure at least 1 post has at least 1 tag. We will be using this for the latter part.
HTML/CSS Template
Now that we have a WordPress setup. Let us start creating the base template of our website. I will be using the Holy Grail layout from this article at Alligator.io
Holy Grail is a layout pattern that’s very common on the web. It consists of a header, a main content area with fixed-width navigation on the left, content in the middle and a fixed-width sidebar on the right and then a footer.
Here is the demo I built on Codepen.io. We will be using this as our layout on GatsbyJS.
GatsbyJS
If you are following this guide, I assume you have NodeJS setup on your machine. If not, here is a step by step guide.
GatsbyJS is ReactJS under the hood so it would be easier to follow this if you have basic understanding of React or frontend frameworks in general. Here is a good starter to ReactJS Basics by Maximilian Schwarzmüller. Don’t worry, you should still be able to follow this guide.
Setting up GatsbyJS
We will be using the gatsby-starter-wordpress. Here gatsbyhg
is the name of our project, you may use any name you want.
1npx gatsby new gatsbyhg https://github.com/GatsbyCentral/gatsby-starter-wordpress
You can preview what’s on this gatsby starter template by running it
1yarn start
Then, navigate to http://localhost:8000/
Connecting to our WordPress site
To connect to our WordPress site. Open the gatsby-config.js
file. Look for baseUrl
, modify it to point to your site without the https://
. Start the project and check if the content displayed is indeed from your WordPress site.
yarn start
Integrating our HTML/CSS template
The gatsby-starter-wordpress already has a template built with it. We will replace this with our Holy Grail layout.
First, let’s edit the src/components/Layout.js
to match our layout.
We will be specifically modifying this portion:
1<div>2 <Helmet title="Home | Gatsby + WordPress" />3 <Navbar />4 <div>{children}</div>5</div>
To this :
1<div className="layout">2 <Helmet title="Home | Gatsby + WordPress" />3 <header>Header</header>45 <Navbar />67 <main>{children}</main>89 <aside>Sidebar</aside>1011 <footer>Footer</footer>12</div>
Next we add a CSS file called style.css
under src/components/
with the following content:
1/* Layout */23.layout {4 display: grid;56 grid-template-areas:7 'header header header'8 'nav content side'9 'footer footer footer';1011 grid-template-columns: 200px 1fr 200px;12 grid-template-rows: auto 1fr auto;13 grid-gap: 10px;1415 height: 100vh;16}1718header {19 grid-area: header;2021 display: flex;22 justify-content: space-between;23}2425nav {26 grid-area: nav;27 margin-left: 0.5rem;28}2930main {31 grid-area: content;32}3334aside {35 grid-area: side;36 margin-right: 0.5rem;37}3839footer {40 grid-area: footer;41}4243/* Theme */44/* This is optional, you can use your own style for this */4546body {47 padding: 0;48 margin: 0;49 font-family: -apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI', Roboto,50 'Helvetica Neue', Arial, sans-serif;51}5253header {54 font-size: 18px;55 color: #fff;56 min-height: 20px;57 background: #0a2b4e;58 padding: 20px;59}6061nav,62aside {63 background: #666;64 padding: 10px;65 color: #fff;66}6768footer {69 background: black;70 color: #fff;71 padding: 20px;72}7374.navbar-item {75 display: inline-block;76 color: #fff;77 padding: 10px;78}7980.navbar-item img {81 height: 20px;82}8384.pagination {85 margin: 0;86 background: black;87}8889.pagination a {90 color: #fff;91}
Modify style imports on src/components/Layout.js
1import './style.css' // add this2import './style/all.sass' // remove this, we won't be needing this anymore