Query WordPress 数据
Query WordPress 数据区块

区块

在指南 使用(Gutenberg)区块 中阅读更多内容。

以下是获取区块数据的 Query 示例。

通过 Block 类型获取自定义文章中的区块

获取文章中所有区块的数据:

{
  post(by: { id: 19 }) {
    blocks {
      ...BlockData
    }
  }
}
 
fragment BlockData on Block {
  name
  attributes
  # innerHTML
  contentSource
  innerBlocks {
    name
    attributes
    # innerHTML
    contentSource
    innerBlocks {
      name
      attributes
      # innerHTML
      contentSource
      innerBlocks {
        name
        attributes
        # innerHTML
        contentSource
        innerBlocks {
          name
          attributes
          # innerHTML
          contentSource
          innerBlocks {
            name
            attributes
            # innerHTML
            contentSource
            innerBlocks {
              name
              attributes
              # innerHTML
              contentSource
              innerBlocks {
                name
                attributes
                # innerHTML
                contentSource
              }
            }
          }
        }
      }
    }
  }
}

仅获取特定类型的区块:

{
  post(by: { id: 19 }) {
    blocks(
      filterBy: {
        include: [
          "core/heading",
          "core/paragraph"
        ]
      }
    ) {
      ...BlockData
    }
  }
}
 
fragment BlockData on Block {
  name
  attributes
  # innerHTML
  contentSource
  innerBlocks {
    name
    attributes
    # innerHTML
    contentSource
    innerBlocks {
      name
      attributes
      # innerHTML
      contentSource
      innerBlocks {
        name
        attributes
        # innerHTML
        contentSource
        innerBlocks {
          name
          attributes
          # innerHTML
          contentSource
          innerBlocks {
            name
            attributes
            # innerHTML
            contentSource
            innerBlocks {
              name
              attributes
              # innerHTML
              contentSource
              innerBlocks {
                name
                attributes
                # innerHTML
                contentSource
              }
            }
          }
        }
      }
    }
  }
}

排除区块:

{
  post(by: { id: 19 }) {
    blocks(
      filterBy: {
        exclude: [
          "core/heading",
          "core/paragraph"
        ]
      }
    ) {
      ...BlockData
    }
  }
}
 
fragment BlockData on Block {
  name
  attributes
  # innerHTML
  contentSource
  innerBlocks {
    name
    attributes
    # innerHTML
    contentSource
    innerBlocks {
      name
      attributes
      # innerHTML
      contentSource
      innerBlocks {
        name
        attributes
        # innerHTML
        contentSource
        innerBlocks {
          name
          attributes
          # innerHTML
          contentSource
          innerBlocks {
            name
            attributes
            # innerHTML
            contentSource
            innerBlocks {
              name
              attributes
              # innerHTML
              contentSource
              innerBlocks {
                name
                attributes
                # innerHTML
                contentSource
              }
            }
          }
        }
      }
    }
  }
}

通过 JSONObject 类型获取自定义文章中的区块数据

获取文章中所有区块的数据:

{
  posts(by: { id: 19 }) {
    blockDataItems
  }
}

仅获取特定类型的区块:

{
  posts(by: { id: 19 }) {
    blockDataItems(
      filterBy: {
        include: [
          "core/heading",
          "core/paragraph"
        ]
      }
    )
  }
}

排除区块:

{
  posts(by: { id: 19 }) {
    blockDataItems(
      filterBy: {
        exclude: [
          "core/heading",
          "core/paragraph"
        ]
      }
    )
  }
}

获取自定义文章中扁平化的区块数据

字段 blockFlattenedDataItems 将自定义文章中包含的区块层级结构扁平化为单一层级。因此,按区块类型过滤时,也会包含父区块被排除的内部区块。

获取文章中所有区块的数据:

{
  posts(by: { id: 19 }) {
    blockFlattenedDataItems
  }
}

仅获取特定类型的区块:

{
  posts(by: { id: 19 }) {
    blockFlattenedDataItems(
      filterBy: {
        include: [
          "core/heading",
          "core/paragraph",
          "core/columns",
          "core/column"
        ]
      }
    )
  }
}