question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Having a hard time with Featured Images

See original GitHub issue

I’m really struggling to pull through featured images. I’m hoping I’m just doing something stupid or missing something.

So this is my query in PostList.js:

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 {
      localFile {
        childImageSharp {
          fluid(maxWidth: 1200){
            ...GatsbyImageSharpFluid
        }
      }
    }
  }
}
`

And I don’t know if I’m wrong, but this is how I get my image:

<Img fluid={post.featured_media.localFile.childImageSharp.fluid}  />

And I’ve even tried:

<img src={post.featured_media.localFile.childImageSharp.src} />

What I end up with is an error that says:

TypeError: Cannot read property 'localFile' of null

And if I console.log(posts), I can even see the images:

featured_media:
localFile:
childImageSharp:
fluid:
aspectRatio: 1.7777777777777777
base64: "data:image/jpeg;base64,/9j/2wBDABALDA4MCh...etc etc 
sizes: "(max-width: 800px) 100vw, 800px"
src: "/static/cdffa355894ff255a99fe00bb5586590/b17c1/outsource-online-marketing1.jpg"
srcSet: "/static/cdffa355894ff255a99fe00bb5586590/00e5e/outsource-onl

I would be so grateful if someone could maybe just point me in the right direction!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
chmaccommented, Jan 17, 2020

@ibjorn If you copy and paste exactly this into the GraphiQL explorer, it should not return any data:

  fragment PostListFields on wordpress__POST {
    id
    title
    excerpt
    author {
      name
      slug
      avatar_urls {
        wordpress_48
      }
    }
    date(formatString: "MMMM DD, YYYY")
    slug
    featured_media {
      localFile {
        childImageSharp {
          fluid(maxWidth: 1200){
            ...GatsbyImageSharpFluid
        }
      }
    }
  }
}

The reason is it’s just a fragment. It’s not a query. You also need a query.

What you posted second looks like the correct data. But somehow you need to tell GraphQL which post you want, so you get the correct post data for that page.

Look at the starter here for example:

https://github.com/GatsbyCentral/gatsby-starter-wordpress/blob/0b89898aea7b872011ee23cd380809ccbc6e7548/src/templates/page.js#L49-L56

It has a query ..., the fragment ... part is not enough on its own. You can use the fragment inside of a query.

0reactions
ibjorncommented, Jan 17, 2020

@chmac Yeah, I’m going to have to brush up a bit on that. Gatsby has been my first introduction to GraphQL, so I’m still finding my way around. What I was trying to do is add featured images to the blog feed and category list.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WordPress Featured Image Not Showing Properly (Or Not At All)
Learn how to fix WordPress featured image not showing properly on your site. We'll explore both manual and plugin solutions.
Read more >
I have a trouble with featured image | WordPress.org
Friends I have an issue with my website.The featured image option is not present in my admin panel under the post section Few...
Read more >
WordPress Featured Image Not Showing Error - Hostinger
Incorrect WordPress dashboard settings can cause featured images not to show up in your post and page lists and the WordPress editor. The...
Read more >
WordPress Featured Image Not Showing Properly (Or Not At All)
Featured images matter. A lot! Here's the definitive guide to fix WordPress featured image not showing properly on your site ...
Read more >
Featured Image Not Showing WordPress: 12 Easy Ways to Fix
To avoid the problem, make sure you're using featured images instead of cover images. The Featured image section is in the right-hand Settings ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found