WordPress-gegevens opvragenMenu
Menu
Dit zijn voorbeelden van queries om menugegevens op te halen.
Menu's ophalen
Haal een specifiek menu op en de ruwe gegevens van de items:
{
menu(by: { id: 176 }) {
itemDataEntries
}
}Filteren op eigenschappen van menu-items via include of exclude:
{
menu(by: { id: 176 }) {
itemDataEntries(propertiesBy: { exclude: ["localURLPath", "rawLabel"] })
}
}Haal alle menu's op en nest queries om eigenschappen van de items te selecteren:
{
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
}Menu's filteren en pagineren:
{
menus(pagination: { limit: 1, offset: 1}, filter: { search: "all" }) {
id
name
slug
}
menuCount(filter: { search: "all" })
}Menu's aanmaken
Alleen beheerders (of gebruikers met de bevoegdheid edit_theme_options) kunnen menu's aanmaken of bijwerken.
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
}Menu's bijwerken
Werk de menulocaties bij:
mutation UpdateMenu {
updateMenu(input: {
id: 176
locations: ["footer", "footer-mobile"]
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
menu {
id
locations
}
}
}Deze query gebruikt geneste mutaties om de naam van het menu bij te werken:
mutation {
menu(by: { id: 176 }) {
originalName: name
update(input: {
name: "Mobile header menu"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
menu {
newName: name
}
}
}
}Prev
Next