Truy vấn dữ liệu WordPressMenu
Menu
Đây là các ví dụ về queries để lấy dữ liệu menu.
Lấy menu
Lấy một menu cụ thể và dữ liệu thô của các mục trong menu đó:
{
menu(by: { id: 176 }) {
itemDataEntries
}
}Lọc thuộc tính của các mục menu qua include hoặc exclude:
{
menu(by: { id: 176 }) {
itemDataEntries(propertiesBy: { exclude: ["localURLPath", "rawLabel"] })
}
}Lấy tất cả menu, lồng các queries để chọn thuộc tính từ các mục:
{
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
}Lọc và phân trang menu:
{
menus(pagination: { limit: 1, offset: 1}, filter: { search: "all" }) {
id
name
slug
}
menuCount(filter: { search: "all" })
}Tạo menu
Chỉ người dùng quản trị (hoặc những người có quyền edit_theme_options) mới có thể tạo hoặc cập nhật menu.
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
}Cập nhật menu
Cập nhật vị trí của menu:
mutation UpdateMenu {
updateMenu(input: {
id: 176
locations: ["footer", "footer-mobile"]
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
menu {
id
locations
}
}
}Query này sử dụng các mutation lồng nhau để cập nhật tên của menu:
mutation {
menu(by: { id: 176 }) {
originalName: name
update(input: {
name: "Mobile header menu"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
menu {
newName: name
}
}
}
}Prev
Next