Query WordPress 数据
Query WordPress 数据菜单

菜单

以下是获取菜单数据的 Query 示例。

获取菜单

获取特定菜单及其条目的原始数据:

{
  menu(by: { id: 176 }) {
    itemDataEntries
  }
}

使用 includeexclude 过滤菜单项属性:

{
  menu(by: { id: 176 }) {
    itemDataEntries(propertiesBy: { exclude: ["localURLPath", "rawLabel"] })
  }
}

获取所有菜单,并通过嵌套 Query 选择条目的属性:

{
  menus {
    id
    name
    slug
    count
    locations
    items {
      ...MenuItemData
      children {
        ...MenuItemData
        children {
          ...MenuItemData
        }
      }
    }
  }
}
fragment MenuItemData on MenuItem {
  id
  itemType
  objectType
  objectID
  parentID
  localURLPath
  label
  rawLabel
  titleAttribute
  url
  cssClasses
  target
  description
  linkRelationship
}

过滤和分页菜单:

{
  menus(pagination: { limit: 1, offset: 1}, filter: { search: "all" }) {
    id
    name
    slug
  }
  menuCount(filter: { search: "all" })
}

创建菜单

只有管理员用户(或拥有 edit_theme_options 权限的用户)才能创建或更新菜单。

mutation CreateMenu {
  createMenu(input: {
    name: "Header menu"
    locations: ["header"]
    itemsBy: { json: [
      {
        label: "Custom parent (nested)",
        itemType: custom,
        url: "https://www.example.com/parent",
        titleAttribute: "Parent title attribute",
        description: "Parent menu item description",
        cssClasses: ["menu-item", "menu-item-parent"],
        target: "_blank",
        linkRelationship: "nofollow",
        children: [
          {
            label: "Custom child",
            itemType: custom,
            url: "https://www.example.com/parent/child",
            description: "Child menu item description",
            cssClasses: ["menu-item", "menu-item-child"],
            target: "_self",
            linkRelationship: "follow"
          },
          {
            label: "Page child",
            itemType: post_type,
            objectType: "page",
            objectID: 2,
            titleAttribute: "Go to sample page",
            description: "Page child description",
            cssClasses: ["menu-item", "menu-item-page"],
            target: "_blank",
            linkRelationship: "nofollow"
          },
          {
            label: "Category child",
            itemType: taxonomy,
            objectType: "category",
            objectID: 3
          }
        ]
      },
      {
        label: "Root page item",
        itemType: post_type,
        objectType: "page",
        objectID: 2
      },
      {
        label: "Root category item",
        itemType: taxonomy,
        objectType: "category",
        objectID: 5
      },
      {
        label: "Custom root item",
        itemType: custom,
        url: "https://www.example.com/2",
        description: "Custom root item description",
        cssClasses: ["menu-item", "menu-item-root"],
        target: "_self",
        linkRelationship: "follow",
        children: [
          {
            label: "Custom grandchild",
            itemType: custom,
            url: "https://www.example.com/2/grandchild",
            description: "Custom grandchild description",
            cssClasses: ["menu-item", "menu-item-grandchild"],
            target: "_blank",
            linkRelationship: "nofollow"
          }
        ]
      }
    ] }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    menu {
      id
      name
      slug
      count
      locations
      itemDataEntries
      items {
        ...MenuItemData
        children {
          ...MenuItemData
          children {
            ...MenuItemData
            children {
              ...MenuItemData
            }
          }
        }
      }
    }
  }
}
 
fragment MenuItemData on MenuItem {
  id
  itemType
  objectType
  objectID
  parentID
  localURLPath
  label
  rawLabel
  titleAttribute
  url
  cssClasses
  target
  description
  linkRelationship
}

更新菜单

更新菜单的显示位置:

mutation UpdateMenu {
  updateMenu(input: {
    id: 176
    locations: ["footer", "footer-mobile"]
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    menu {
      id
      locations
    }
  }
}

此 Query 使用嵌套 mutation 更新菜单的名称:

mutation {
  menu(by: { id: 176 }) {
    originalName: name
    update(input: {
      name: "Mobile header menu"
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      menu {
        newName: name
      }
    }
  }
}