如何从 Gatsby WordPress 网站的页面内容查询中排除作者和日期

问题描述

我正在制作一个显示 wordpress 页面内容的 Gatsby 站点(使用 WP 插件 QP Gatsby 和 WPgraphQL),并且我能够定位填充页面所需的大部分数据(标题、图像等),但我在页面内容方面遇到麻烦。

我使用 dangerouslySetInnerHtml显示页面内容,有时 Gatsby 站点会在段落文本上方显示页面作者和页面日期。显示作者和日期适用于博客文章,但在我的“关于”页面顶部看到“by John Doe,2020 年 6 月 2 日”很奇怪。有时页面内容会适当地呈现(没有作者和日期),我认为我观察到的模式是在 gatsby develop 运行时在 wordpress 网站上编辑页面内容后出现作者和日期.

有没有办法从页面内容查询中排除作者和日期? screenshot of page example

const IndexPage = ({ data }) => (
  <Layout>
    <article>
      {data.wpPage.featuredImage && (
        <figure>
          <Img
            fluid={data.wpPage.featuredImage.node.localFile.childImageSharp.fluid}
            alt={data.wpPage.featuredImage.node.altText}
          />
        </figure>
      )}
      <div dangerouslySetInnerHTML={{ __html: data.wpPage.content }} />
    </article>
  </Layout>
)

export const query = graphql`
  query HomePageQuery {
    wpPage(uri: {eq: "/"}) {
      id
      title
      content
      featuredImage {
        node {
          localFile {
            childImageSharp {
              fluid(maxWidth: 1360) {
                ...GatsbyImageSharpFluid
              }
            }
          }
          altText
        }
      }
    }
  }
`

解决方法

当您从 WP 端更新数据库且 gatsby develop 仍在运行时,就会发生这种情况。

如果您中断并重新启动 gatsby develop,dangerousSetInnerHTML 内容应该正确显示。