|
| 1 | +## How to create a site with data pulled from WordPress |
| 2 | + |
| 3 | +### What this tutorial covers: |
| 4 | + |
| 5 | +In this tutorial, you will install the `gatsby-source-wordpress` plugin in order to pull blog and image data from a WordPress install into your Gatsby site and render that data. This [Gatsby + Wordpress demo site](https://using-wordpress.gatsbyjs.org/) shows you a sample of what you’re going to be building in this tutorial., although it’s missing the cool images you’ll be adding :D |
| 6 | + |
| 7 | +### Why go through this tutorial? |
| 8 | + |
| 9 | +While each source plugin may operate differently from others, it’s worth going through this tutorial because you will almost definitely be using a source plugin in most Gatsby sites you build. This tutorial will walk you through the basics of connecting your Gatsby site to a CMS, pulling in data, and using React to render that data in beautiful ways on your site. |
| 10 | + |
| 11 | +If you’d like to look at the growing number source plugins available to you, search for “source” in the [Gatsby plugin library](/plugins/?=source). |
| 12 | + |
| 13 | +### Creating a site with the `gatsby-source-wordpress` plugin |
| 14 | + |
| 15 | +Create a new Gatsby project and change directories into the new project you just created: |
| 16 | + |
| 17 | +```shell |
| 18 | +gatsby new wordpress-tutorial-site |
| 19 | +cd wordpress-tutorial-site |
| 20 | +``` |
| 21 | + |
| 22 | +Install the `gatsby-source-wordpress` plugin. For extra reading on the plugin’s features and examples of GraphQL queries not included in this tutorial, see the [`gatsby-source-wordpress` plugin’s READme file](/packages/gatsby-source-wordpress/?=wordpress). |
| 23 | + |
| 24 | +```shell |
| 25 | +npm install --save gatsby-source-wordpress |
| 26 | +``` |
| 27 | + |
| 28 | +Add the `gatsby-source-wordpress` plugin to `gatsby-config.js` using the following code, which you can also find in the [demo site’s source code](https://github.com/gatsbyjs/gatsby/blob/master/examples/using-wordpress/gatsby-config.js). |
| 29 | + |
| 30 | +```javascript{32-58} |
| 31 | + module.exports = { |
| 32 | + siteMetadata: { |
| 33 | + title: 'Gatsby Wordpress Tutorial', |
| 34 | + }, |
| 35 | + plugins: [ |
| 36 | + // https://public-api.wordpress.com/wp/v2/sites/gatsbyjsexamplewordpress.wordpress.com/pages/ |
| 37 | + /* |
| 38 | + * Gatsby's data processing layer begins with “source” |
| 39 | + * plugins. Here the site sources its data from Wordpress. |
| 40 | + */ |
| 41 | + { |
| 42 | + resolve: `gatsby-source-wordpress`, |
| 43 | + options: { |
| 44 | + /* |
| 45 | + * The base URL of the Wordpress site without the trailingslash and the protocol. This is required. |
| 46 | + * Example : 'gatsbyjswpexample.wordpress.com' or 'www.example-site.com' |
| 47 | + */ |
| 48 | + baseUrl: `dev-gatbsyjswp.pantheonsite.io`, |
| 49 | + // The protocol. This can be http or https. |
| 50 | + protocol: `http`, |
| 51 | + // Indicates whether the site is hosted on wordpress.com. |
| 52 | + // If false, then the asumption is made that the site is self hosted. |
| 53 | + // If true, then the plugin will source its content on wordpress.com using the JSON REST API V2. |
| 54 | + // If your site is hosted on wordpress.org, then set this to false. |
| 55 | + hostingWPCOM: false, |
| 56 | + // If useACF is true, then the source plugin will try to import the Wordpress ACF Plugin contents. |
| 57 | + // This feature is untested for sites hosted on Wordpress.com |
| 58 | + useACF: true, |
| 59 | + }, |
| 60 | + }, |
| 61 | + ], |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### Creating GraphQL queries that pull data from WordPress |
| 66 | + |
| 67 | +Now you are ready to create a GraphQL query to pull in some data from the WordPress site. You will create a query that pulls in the title of the blogposts, date they were posted, and blogpost content. |
| 68 | + |
| 69 | +Run: |
| 70 | + |
| 71 | +```shell |
| 72 | +gatsby develop |
| 73 | +``` |
| 74 | + |
| 75 | +Open localhost:8000 and localhost:8000/__graphql. |
| 76 | + |
| 77 | +This query will pull in the blogpost content from WordPress: |
| 78 | + |
| 79 | +```graphql |
| 80 | +query { |
| 81 | + allWordpressPage { |
| 82 | + edges { |
| 83 | + node { |
| 84 | + id |
| 85 | + title |
| 86 | + excerpt |
| 87 | + slug |
| 88 | + date(formatString: "MMMM DD, YYYY") |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | + |
| 96 | +This query will pull in a sorted list of those blogposts: |
| 97 | + |
| 98 | +```graphql |
| 99 | +{ |
| 100 | + allWordpressPost(sort: { fields: [date] }) { |
| 101 | + edges { |
| 102 | + node { |
| 103 | + title |
| 104 | + excerpt |
| 105 | + Slug |
| 106 | + ...PostIcons |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | + |
| 114 | +### Rendering the blogposts to `index.js` |
| 115 | + |
| 116 | +Here is what your `index.js` should look like: |
| 117 | + |
| 118 | +```jsx |
| 119 | +import React from 'react' |
| 120 | + |
| 121 | +export default ({ data }) => { |
| 122 | + console.log(data) |
| 123 | + return ( |
| 124 | + <div> |
| 125 | + <h1>My WordPress Blog</h1> |
| 126 | + <h4>Posts</h4> |
| 127 | + {data.allWordpressPost.edges.map(({ node }) => ( |
| 128 | + <div> |
| 129 | + <p>{node.title}</p> |
| 130 | + <p>{node.excerpt}</p> |
| 131 | + </div> |
| 132 | + ))} |
| 133 | + </div> |
| 134 | + )T |
| 135 | +} |
| 136 | + |
| 137 | +// Set here the ID of the home page. |
| 138 | +export const pageQuery = graphql` |
| 139 | + query MyFiles { |
| 140 | + allWordpressPost(sort: { fields: [date] }) { |
| 141 | + edges { |
| 142 | + node { |
| 143 | + title |
| 144 | + excerpt |
| 145 | + slug |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +` |
| 151 | +``` |
| 152 | + |
| 153 | +[Note to editors: it would be useful to insert a screenshot of the final result here] |
| 154 | + |
| 155 | +### Create slugs for each blogpost and link to them from `index.js` |
| 156 | +[Part 7](/tutorial/part-seven/) of the foundational tutorial goes through this process. |
0 commit comments