Guest User

gatsby-node

a guest
Jan 31st, 2018
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const _ = require(`lodash`)
  2. const Promise = require(`bluebird`)
  3. const path = require(`path`)
  4. const slash = require(`slash`)
  5.  
  6. // Implement the Gatsby API “createPages”. This is
  7. // called after the Gatsby bootstrap is finished so you have
  8. // access to any information necessary to programmatically
  9. // create pages.
  10. // Will create pages for Wordpress pages (route : /{slug})
  11. // Will create pages for Wordpress posts (route : /post/{slug})
  12. exports.createPages = ({ graphql, boundActionCreators }) => {
  13.   const { createPage } = boundActionCreators
  14.   return new Promise((resolve, reject) => {
  15.     // The “graphql” function allows us to run arbitrary
  16.     // queries against the local Wordpress graphql schema. Think of
  17.     // it like the site has a built-in database constructed
  18.     // from the fetched data that you can run queries against.
  19.  
  20.       // ==== POSTS (WORDPRESS NATIVE AND ACF) ====
  21.       graphql(
  22.         `
  23.           {
  24.             allWordpressPost {
  25.               edges {
  26.                 node {
  27.                   id
  28.                   slug
  29.                   status
  30.                   template
  31.                   format
  32.                 }
  33.               }
  34.             }
  35.           }
  36.         `
  37.       ).then(result => {
  38.         if (result.errors) {
  39.           console.log(result.errors)
  40.           reject(result.errors)
  41.         }
  42.         const postTemplate = path.resolve(`./src/templates/Single.js`)
  43.         // We want to create a detailed page for each
  44.         // post node. We'll just use the Wordpress Slug for the slug.
  45.         // The Post ID is prefixed with 'POST_'
  46.         _.each(result.data.allWordpressPost.edges, edge => {
  47.           createPage({
  48.             path: edge.node.slug,
  49.             component: slash(postTemplate),
  50.             context: {
  51.               id: edge.node.id,
  52.             },
  53.           })
  54.         })
  55.         resolve()
  56.       })
  57.     // ==== END POSTS ====
  58.   })
  59. }
Advertisement
Add Comment
Please, Sign In to add comment