Access Page and Custom Field data from Menu Items
masterWhen iterating through the menu items returned by all(), you can access specific attributes on each $item:
- Page/Post ID: Use
$item->objectIdto get the ID of the linked page or post. This is useful for functions likeget_post_type(). - Custom Fields: Use
$item->id(the menu item's unique ID) to retrieve custom fields attached to that specific menu item via plugins like ACF.
// Get the post type of the linked page
$type = get_post_type($item->objectId);
// Get a custom field (e.g., via ACF) attached to the menu item
$label = get_field('custom_menu_label', $item->id) ?: $item->label;