Search code examples
reactjswordpressgatsbyheadless-cms

How to render all wordpress pages as a section in a single page gatsby site


I am wanting to build a single page portfolio that is structured in sections like: intro projects CV contact

I have a separate page for each of those sections created in wordpress. How can I render each wordpress page to one single page with gatsby?

Here is where im creating gatsby pages from wordpress API: https://github.com/joeymorello/port-site/blob/master/gatsby-node.js


Solution

  • A very simple example might be adding something something like this to your pages/index.js :

    import React from "react"
    import { graphql } from "gatsby"
    
    const IndexPage = ({ data }) => {
      const { allWordpressPage } = data
      return (
      <>
        {allWordpressPage.edges.node.map(node => {
          const { title, content, id } = node
          return (
            <section key={ id }>
              <h2>{{ title }}</h2>
              <div>{{ content }}</div>
            </section>
          )
        })}
      </>
    }
    )}
    
    export default IndexPage
    
    
    
    export const pageQuery = graphql`
      query {
        allWordpressPage {
          edges {
            node {
              id
              title
              content
            }
          }
        }
      }
    `