wagtailmenus Documentation
repository·master·Indexed 19 days ago
https://github.com/jazzband/wagtailmenusA Wagtail CMS extension for managing and rendering multi-level and flat navigation menus. It provides tools for creating link pages via AbstractLinkPage and offers extensibility through abstract models like AbstractMainMenu, AbstractMainMenuItem, AbstractFlatMenu, and AbstractFlatMenuItem to allow custom fields and logic in menu structures.
What's inside wagtailmenus
- wagtailmenus is an extension for Wagtail CMS designed to help manage and render multi-level navigation and simple flat menus. It provides a consistent and flexible way to handle site navigation within the Wagtail ecosystem.
What is the AbstractLinkPage model?
masterThe
AbstractLinkPagemodel is an abstract model designed to allow editors to add additional links to menus by adding pages to the Wagtail page tree.This is useful because standard 'main' and 'flat' menus only allow editors to define top-level items. By using
AbstractLinkPage, editors can create link pages that point to other Wagtail pages or custom URLs.Key behaviors:
- Automatic Visibility: If a link page points to another Wagtail page, the link automatically hides if the target page is unpublished, expires, or is set to not show in menus. It automatically reappears when the target page is published or updated to show in menus again.
- Tree Constraints: By default, link pages do not allow child pages.
- SEO/Search: Link pages are excluded from Wagtail-generated sitemaps and search results by default.
Allow menu items to repeat alongside their children
masterNormally, in a multi-level menu, a parent item acts as a toggle. If you want an important page to appear as a clickable link and also act as a parent for a sub-menu, extend the
wagtailmenus.models.MenuPagemodel instead of the standardwagtail.core.models.Pagemodel.This provides extra fields that allow the item to repeat in the rendered menu, ensuring it doesn't just become a non-clickable 'toggle'.
Configure custom URLs and anchors in menus
masterWhen defining menu items, you are not limited to standard page links. You can use the custom URL field to:
- Link to specific anchors on a page (e.g.,
#signupor#request-callback). - Include fixed GET parameters for analytics or custom JS functionality.
- Combine page links with custom URL suffixes.
- Link to specific anchors on a page (e.g.,
Handle menus in multi-site projects
masterWagtailmenus supports multi-site setups through several strategies for flat menus:
- Per-site menus: Define a unique menu for every site.
- Shared menus: Define a menu for the default site and reuse it across all other sites.
- Hybrid approach: Mix and match, using site-specific menus for some and the default site's menu for others.
Key features for multi-site users:
- Copy feature: Quickly copy existing menus from one site to another via the CMS interface.
- Custom templates: Configure separate sets of templates for each site (see
custom_templates_auto). - Fallback logic: Use the
fall_back_to_default_site_menusoption to control behavior when a site-specific menu is missing.
Control page visibility in menus with show_in_menus
masterEven if a page is a child of a menu item with
allow_subnav=True, it will only appear in the rendered menu if itsshow_in_menusattribute isTrue.To exclude specific page types from appearing in menus by default (e.g., news articles or events that should only appear on listing pages), set the
show_in_menus_defaultattribute toFalseon your page type class:class NewsArticlePage(Page): show_in_menus_default = False # ... other fieldsManipulate sub-menu items by subclassing MenuPage or MenuPageMixin
masterWhen a page model subclasses
MenuPageorMenuPageMixin, it receives special treatment from thewagtailmenustemplate tags. This allows you to programmatically add, remove, or modify sub-menu items that appear below that specific page in a menu.To implement custom sub-menu logic, you must override two methods:
modify_submenu_items(self, menu_items, **kwargs): Used to add or alter the list of menu items. You should always callsuper().modify_submenu_items(menu_items, **kwargs)first to ensure default behavior (like repeating links) is preserved.has_submenu_items(self, **kwargs): Used to tell the menu template whether this page should render a dropdown/submenu. If you are adding items manually viamodify_submenu_items, you often need to override this to returnTrueeven if the page has no child pages.
Important Note: If you override
modify_submenu_items, ensure that 'repeated menu items' remain the first item in the returned list to prevent issues with active class highlighting.class MyPage(MenuPage): def modify_submenu_items(self, menu_items, **kwargs): menu_items = super().modify_submenu_items(menu_items, **kwargs) # Add custom items here return menu_items def has_submenu_items(self, **kwargs): return TrueManage additional menus as 'flat menus'
masterBeyond the main navigation, you can create any number of additional menus (like footers or secondary headers) via the CMS as 'flat menus'.
Flat menus are managed independently in the CMS, allowing editors to make 'emergency changes' or 'last-minute tweaks' without code deployments. Although called 'flat', they can still be configured to render as multi-level menus if required.
Override sub_menu rendering behavior
masterYou can customize the behavior of nested menus using specific arguments within the
{% sub_menu %}tag:use_absolute_page_urls: Set toTrueto use absolute URLs forhrefattributes instead of relative ones.template: Pass a string representing a template path to override the default sub-menu template (which is normally controlled bysub_menu_templatein the context).apply_active_classes: Override whether active CSS classes are applied.allow_repeating_parents: Override whether parent items are allowed to repeat in the menu structure.
How MenuPage and MenuPageMixin solve navigation toggle issues
masterIn multi-level menus, parent pages often act only as toggles to show/hide children, making the parent page itself inaccessible.
By using
MenuPageorMenuPageMixin, you can enable a feature called Repeat in sub-navigation. When enabled, an additional link is rendered alongside the children in the menu, allowing users to access the parent page directly.You can customize the text for this repeated link using the Repeated item link text field in the page settings. The menu tags are designed to handle 'active' classes intelligently: when viewing the parent page, the repeated item is marked as 'active' while the original parent link is marked as 'ancestor' to maintain consistent styling.
How multi-level menus are generated
masterWagtailmenus uses a hybrid approach to menu structure:
- Top-level selection: You manually choose which pages appear as the top-level items for a menu in the CMS.
- Dynamic sub-menus: For any top-level item where
allow_subnav=Trueis set, the library dynamically generates the sub-menu structure based on your existing Wagtail page tree.
This ensures that while you control the main navigation entry points, the sub-navigation automatically stays in sync with the natural order and structure of your page tree, reducing manual maintenance when pages are moved or reordered.
Getting started with wagtailmenus
masterwagtailmenus is an extension for Wagtail CMS designed to help you define, manage, and render menus. To begin using the project, follow these primary paths:
- Setup: Follow the
installationguide to add the package to your Wagtail project. - Menu Management: Learn how to manage different menu types using
managing_main_menus(for hierarchical structures) ormanaging_flat_menus(for simpler structures). - Rendering: Use the provided template tags or custom templates to display menus in your site's frontend.
- Page Models: Optionally use specialized page models like
MenuPageorAbstractLinkPageto integrate menu management directly into your Wagtail page tree.
- Setup: Follow the