wordpress - 用于高级自定义字段的 Gatsby 背景图像和 WPGraphQL
问题描述
我正在尝试使用gatsby-background-image插件在 Gatsby 中设置背景。
我的查询返回 sourceUrl 和 alt-text 就好了。但是,当运行 gatsby develop 时,它给了我一个错误“TypeError:无法读取未定义的属性 'hero'”。做一些研究我想我可能不得不使用 ChildImageSharp 来指定流体。但是,我不确定 ACF 字段是否可行?
import React from "react"
import BackgroundImage from "gatsby-background-image"
import { Link, graphql } from "gatsby"
import Layout from "../components/layout"
import SEO from "../components/seo"
const IndexPage = (props) => (
<Layout>
<SEO title="Home" />
<BackgroundImage
className="masthead"
fadeIn
fluid={props.data.wordpress.pages.nodes.undersideACFgraphql.hero.sourceUrl}
>
<div className="black-overlay">
<div className="content-box">
<h1>This is where my h1 tag goes</h1>
<h2>This is my sub head</h2>
</div>
</div>
</BackgroundImage>
<Link to="/page-2/">Go to page 2</Link>
</Layout>
)
export default IndexPage
export const pageQuery = graphql`
query MyQuery {
wordpress {
pages {
nodes {
undersideACFgraphql {
hero {
sourceUrl
altText
}
}
}
}
}
}
`;
解决方案
推荐阅读
- python - 如何解决此错误:AttributeError:'NoneType' 对象没有属性'_inbound_nodes'?
- javascript - 如何将 0 传递给 TypeScript 可选参数?
- mysql - 在 Superset 时间列中使用整数年份
- mysql - 带有简单游标的简单存储过程不会编译
- python-3.x - 如何循环python游戏但也让它更干净?
- docker - 错误:不允许附加属性 db-migrator
- node.js - 为什么在 Sequelize 中创建关联模型时使用大写键?
- java - 如何使用 nanoTime 准确预测时间?
- swift - Mapkit 获取地标的当前坐标
- google-maps - 如何在颤动中更改谷歌地图 API 的“我的位置”按钮位置?