Hi guys does anyone know how I might go about creating a dynamic navigation using a global with the navigation items as fields, is this even possible?
1 Answer
Global is not really the best option for dynamic navigation, because by definition it is designed to contain just one of something — and while it supports multiple fields, it only supports one instance of each field. Technically you could use a field within the global that supports multiple items (i.e. table
fieldtype or matrix
fieldtype), but none of the fieldTypes support hierarchical structures natively, so I don't see the advantage. Or you could simply paste static HTML into a text field, but I wouldn't really classify that as dynamic. Overall, not a particularly good option.
You should probably be looking into using a 'structure' (or to a lesser extent 'categoryGroup'), where each entry would represent a page or nav item, organized hierarchically. In this way you can reuse the same field within each entry (i.e. 'title') and loop through the entries to dynamically generate the nav, with the added benefit of being able to reorganize the nav easily using drag-and-drop.
For more information, see:
- Documentation on using the nav tag.
- Documentation on giving each Entry Type it's own template.
- Answers from previous questions, such as here and here.
- Check out some of the dedicated nav building plugins, such as a&m navigation or craft nav.
-
Thanks for the advice I'm new to craft, I am only looking for a simple nav with no drop downs and I have actually figured out a way of doing it by creating 6 fields, menuLink1, menuLink2 etc, that have a field type of entries and using {{ globalHandle.menuLink1.first().url }} it works fine. What are the disadvantages of this?– SteveCommented May 18, 2015 at 19:10
-
1The disadvantage is that you are creating fields to represent each page/url. What happens when you need 'menuLink7'? Or you decide to change the order? Or want to disable a section temporarily? There is also no way to loop through your nav items (without looping through the fieldLayout, etc.) What you are doing is essentially creating static nav, that just happens to be stored in a database — which is fine but not really taking advantages of the capabilities of craft. Commented May 18, 2015 at 19:28