14 votes

How to write a simple navigation?

I pulled together this brain-dump of 8 different ways of doing navigation. There's probably more. With any method make sure you keep a close eye on the query count and load speed, as some of these ...
  • 4,564
10 votes

How can I build a language switcher?

Thought I would share my language switcher, as it shows languages in their native name. <nav class="nav nav--locale"> <ul> {% set locales = craft.i18n.getSiteLocales() %} ...
  • 301
9 votes

How can I build a language switcher?

I've created a plugin which makes it incredibly easy to add language-switching links to your website... Language Link plugin for Craft CMS Standard Usage Add this to your _layout, or create a ...
  • 23.8k
8 votes
Accepted

Dymanic 'active' class using nav tag looping through a structure

My recommendation is to match the slugs as opposed to the ids as this is much easier to debug. <ul id="slide-in"> {% nav menu in craft.entries.section('mainMenu') %} DEBUG: Entry....
  • 769
7 votes
Accepted

What is the difference between 'nav' and 'for' for looping?

for is part of the native Twig language. for is for general usage... It's the basic, general looping structure that you'll use 99% of the time. You'll find that it closely mirrors for and foreach in ...
  • 23.8k
6 votes
Accepted

Show parent and it's children as a nav

You can access children through the entry's descendants property. {# display event-information #} <ul> <li><a href="{{ entry.url }}">{{ entry.title }}</a> {# display ...
6 votes
Accepted

How are the Craft docs created?

There is a Documentation structured section with three entry types. Heading Only Page Link The Getting Started, Core Concepts, etc. headers are the Heading Only entry type. The only field is has is ...
  • 67k
6 votes
Accepted

Navigation, Structure, Usage, WOES!

First up I'm not sure if I actually need to place any information into the Entry URL Format and Entry Template settings for the section Nope, you can probably just un-check the Entries in this ...
6 votes
Accepted

Can plugin have two links in admin navigation?

The top links in the CP header are often called nav tabs. Beyond the "default" nav tab for your plugin (pointing to the plugin's CP section), it's possible to declare additional tabs via the ...
5 votes

How to target first or last item in a nav children loop

Update Sorry, I originally thought you were building nav and just showing simplified code. If you just want to list the categories then this should work: {% nav category in categories %} {{- ...
5 votes

How can I build a language switcher?

This simple solution has been working brilliantly for me... {% set pageUri = craft.request.url|replace(siteUrl, '') %} <ul> <li><a href="{{ craft.config.siteUrl['en'] ~ pageUri }}"...
  • 23.8k
5 votes
Accepted

Complex Navigation

I think you need to set a conditional for the entry level for the parent, which is 1. {% set entries = craft.entries.section('pages') %} <ul id="nav"> {% nav entry in entries %} &...
5 votes
Accepted

How can i apply "current" class for category nav?

You can get the current Category Model via the pre-populated category variable on all of your category pages. (→ see Routing type no. 4) As you probably know, entry pages come with entry pre-...
  • 36k
5 votes
Accepted

How do you pull out second level structure entries based on their parent?

Ok I've worked this out from above and adding some other code and now it works as below: {% set parent = entry.getAncestors().first() %} {% for subnav in craft.entries.section('mySection')....
  • 2,033
5 votes
Accepted

Show only children of current page in submenu

Ran into the same issues you had. Pulled this from a working site I recently built. Basically you have to get the topmost page. I found entry.getAncestors().level(1).first() works for that no matter ...
  • 6,562
4 votes

How can I add a dynamic "active" CSS class to the navigation on any given page?

I think comparing the entry id's is the best way to go for the current page. But for determining if a certain page is a parent of the current page, I combined answers from @tim-kelty and @carlcs to ...
4 votes
Accepted

Reorder Control Panel Navigation

Thanks to the P&T team and the latests update to Craft (2.3.2640), this is now possible through a plugin! https://github.com/engram-design/CPNav
  • 3,507
4 votes

How to write a simple navigation?

Another easy way to handle a dynamic menu in Craft is to add a Matrix field to a Global. For example, I have a Global called "Main Menu". That Global has a Matrix field called "Menu Item". I then ...
4 votes
Accepted

Building a nav from the top level page of a structure

Untested, but you should be able to do this by using the merge filter in Twig to join the two results together. Get the top level 'landing page' for this section: {% set landingPage = entry....
4 votes

Building a nav from the top level page of a structure

Basically the same as Steve suggested, but I first merged the entries' IDs and then use them as a criteria in another craft.entries call: {% set landing = entry.getAncestors().level(1).first() %} {% ...
  • 36k
4 votes

Custom navigation URLs?

I think you're looking for entry types - when you open a new 'services' entry you could pick from "service" or "service-information" entry types. Each entry type has it's own field layouts. This ...
4 votes
Accepted

Dynamic navigation using a global

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 ...
4 votes

Category Nav tag with control over the depth that {% children %} goes

i did {% set categories = craft.categories.descendantOf(1066) %} And then i did {% nav category in categories.level(2) %} {% endnav %} Always get the answer myself just after i post these damn ...
  • 912
4 votes

Nest Entries from Different Sections

The Team section’s landing page could just be a template living at about/team/index.html. From there if you want to output info about the team members, just fetch it manually: {% for teamMember in ...
4 votes
Accepted

Large, Complex Navigation

It's those two lines that trigger database queries {% set url = node.url|length ? node.url : node.linkedEntry|length ? node.linkedEntry.first().url : node.customUrl %} {% set class = node.children|...
  • 36k
4 votes
Accepted

Creating a sitemap/index page, ordered by A-Z and 0-9

The way to do this is with the group filter. You want to group the entries by the first character of the title. Get the entries as an actual array {% set allEntries = craft.entries.section('artists')...
4 votes
Accepted

Setting plugin subnav to active

Based on the docs and this guide, looks like you'll want to set selectedSubnavItem in your Twig template(s) to whatever the key of your selected subnav item is (moderateentries, notificationmessages, ...
  • 2,966
3 votes
Accepted

How to get an entry title and URI from a segment in order to create a breadcrumb navigation

If your site is mixture of singles, structures and channels then there is no easy way to get the entry titles and or uris from the segments alone. You would have to add some logic based on your known ...
3 votes

Easy Nav Question

You can use the ancestors property (which is a shortcut to getAncestors()). {% set targetEntry = entry %} {% if entry.level > 1 %} {% set targetEntry = entry.ancestors.last %} {% endif %} To ...

Only top scored, non community-wiki answers of a minimum length are eligible