Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active October 12, 2019 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shelob9/58713096c3583d7df3b2050db4d54fe5 to your computer and use it in GitHub Desktop.
Save Shelob9/58713096c3583d7df3b2050db4d54fe5 to your computer and use it in GitHub Desktop.

Using WordPress Featured Images In Gatsby

GraphQL Queries For WordPress Posts With Featured Image

query PostsWithFeaturedImage {
  allWordpressPost {
    nodes {
      title
      featured_media {
        title
        caption
        description
        alt_text
        source_url 
      }
    }
  }
}

The key description has HTML you could print to the DOM to have an image tag. Or source_url and alt_text could be used with an image tag.

Use sharp to create fixed arguments

query PostsWithFeaturedImage {
  allWordpressPost {
    nodes {
      title
      featured_media {
        title
        caption
        alt_text
        source_url
        localFile {
          childImageSharp {
            fixed(width: 500, height: 300) {
              src
              srcSet,
              srcSetWebp
            }
          }
        }
      }
    }
  }
}

Final Query with everything I need from posts:

{
  allWordpressPost {
    nodes {
      title
      featured_media {
        title
        caption
        alt_text
        source_url
        localFile {
          childImageSharp {
            fixed(width: 500, height: 300) {
              src
              srcSet
              srcSetWebp
              height
              width
              base64
              tracedSVG
              srcWebp
              srcSetWebp
            }
          }
        }
      }
    }
  }
}
export const pageQuery = graphql`
  fragment PostListFields on wordpress__POST {
    id
    title
    excerpt
    author {
      name
      slug
      avatar_urls {
        wordpress_48
      }
    }
    date(formatString: "MMMM DD, YYYY")
    slug
    featured_media {
      title
      caption
      alt_text
      source_url
      localFile {
        childImageSharp {
          fixed(width: 500, height: 300) {
            src
            srcSet
            srcSetWebp
            height
            width
            base64
            tracedSVG
            srcWebp
            srcSetWebp
          }
        }
      }
    }
  }
`

Fixed and fluid:

{
  allWordpressPost(limit: 1) {
    nodes {
      title
      date(formatString: "MMMM DD, YYYY")
      featured_media {
        localFile {
          childImageSharp {
            fluid(maxWidth: 1200, maxHeight: 300) {
              src
              sizes
              srcSet
              srcWebp
              srcSetWebp
              tracedSVG
              aspectRatio
              presentationWidth
              presentationHeight
              base64
            }
            fixed(width: 500, height: 300) {
              src
              srcSet
              srcSetWebp
              height
              width
              base64
              tracedSVG
              srcWebp
              srcSetWebp
            }
          }
        }
      }
    }
  }
}
  • Create Github Repo
  • Render.com
    • Allow repo
    • Create New Web Service
    • Defaults will build site and auto-deploy
import React, { Fragment } from 'react'
import { useStaticQuery, graphql } from 'gatsby'
import Helmet from 'react-helmet'
import './all.sass'
export default function Layout({ children, title }) {
const { site } = useStaticQuery(graphql`
query HeaderQuery {
site {
siteMetadata {
description
}
}
}
`)
const { description } = site.siteMetadata
return (
<Fragment>
<Helmet
title={title}
meta={[{ name: 'description', content: description }]}
>
<html lang="en" />
</Helmet>
<nav>...</nav>
<main role="main">{children}</main>
</Fragment>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment