14 votes

How to write a simple navigation?

I pulled together this brain-dump of 8 different ways of doing navigation. There are probably more. With any method make sure you keep a close eye on the query count and load speed, as some of these ...
James Smith's user avatar
  • 5,154
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 ...
Lindsey D's user avatar
  • 24k
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 ...
Ethan Thompson's user avatar
4 votes

Add home page to a structure?

From an IA and content-modelling perspective, putting all your site's "pages" in a Structure is fraught with problems, and, in my opinion, does a terrible disservice to the future-friendly approach ...
James Smith's user avatar
  • 5,154
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|...
carlcs's user avatar
  • 36.2k
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')...
Marion Newlevant's user avatar
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, ...
Jalen Davenport's user avatar
  • 3,045
4 votes
Accepted

How can I display second level children only from a specific parent structure item?

yes you can! You may search for a slug craft.entries.slug('slug-of-first-parent'), select the first result .one(), and then the children by .getChildren(). Now you can proceed as you already did and ...
Luke's user avatar
  • 521
3 votes

display entries inside nav

You don't necessarily need to use the nav tag, you can just spin through the entries in a normal {% for entry in entries %} loop, making use of the level property and the getDescendants method. There ...
Steve Holland's user avatar
3 votes
Accepted

Adding a random entry to an array

You can use slice and merge filters to “insert” that random entry into your array of entries. In the following example I’m using the id param to query for the selected entries and I’m also setting ...
carlcs's user avatar
  • 36.2k
3 votes
Accepted

Create a "hidden" page within Structure

Well, a custom field actually does sound like the perfect solution. It's easy and consistent to check on all outputs and areas (sitemap/menu's/other navigations).
chris's user avatar
  • 906
3 votes

Custom or complex nav tag

Input: <ul> {% nav category in craft.categories.group('mainMenu').level(1) %} {% if category.hasDescendants() %} <li>1 Level | {{ category.title }}<ul> {% set subCategories = ...
Nikolay Bronskiy's user avatar
3 votes

How to link to previous/next entries in a section?

For navigating siblings in a structure there's a shortcut: {% if entry.prevSibling %}<a href="{{ entry.prevSibling.url }}">Previous</a>{% endif %} {% if entry.nextSibling %}<a ...
Tamlyn's user avatar
  • 183
3 votes

How can I build a language switcher?

If you happen to be using the SEOmatic plugin for Craft 2.x, it comes with a getLocalizedUrls() function that: Returns an array of localized URLs for the current page request. This handles elements ...
andrew.welch's user avatar
  • 11.6k
3 votes

How can I build a language switcher?

I tweaked the version in the answer above by Christian Steger (https://craftcms.stackexchange.com/a/1794/3835). This version, hides the current language from the list of language links, and displays ...
Justin K's user avatar
  • 309
3 votes
Accepted

Exclude certain entries and its children from navigation

If it’s true that the descendantOf param doesn’t allow to pass in multiple entries, you probably need to work around that issue and grab the descendants for each parent entry individually. {% set ...
carlcs's user avatar
  • 36.2k
3 votes

Equivalent of "EE Pages" module in Craft?

A common approach is to create a Structure for the pages, and use a Content Builder where you can use Matrix blocks of various types to allow the client to build out the pages as they see fit in the ...
andrew.welch's user avatar
  • 11.6k
3 votes
Accepted

Equivalent of "EE Pages" module in Craft?

There are a few different ways to do this but first I'd recommend stepping away from the idea of doing it like EE. Craft has a lot of flexibility in how the content model can be implemented. In Craft,...
Ben Parizek's user avatar
  • 13.4k
3 votes
Accepted

How do I hide children of a disabled parent in a nav?

This is an example of our "quick and dirty" menu macro we used in older projects. It's recursive so it can have as many levels as you want and will only fetch enabled entries. For mobile devices the ...
Robin Schambach's user avatar
3 votes
Accepted

How do I create a structure side navigation that will show active parent list-item and active child list-item

After a long search and lots of trial and error, I've found the perfect solution! This is how I created a dynamic structure navigation : parent/child (2 levels deep) with class="active" on the parent ...
mateostabio's user avatar
3 votes
Accepted

Craft CMS Structure: How can I display parent and child items on an active parent page?

I hope I have understand your problem.. Let's look at your code: One sidenote: It might be good to limit your pages-selection only to active pages: {% set pages = craft.entries.section('about')....
Luke's user avatar
  • 521
3 votes
Accepted

Improve performance and element queries when used multiple times

Since you have two sections, you will need your two initial queries, however, in your example, although you are eager loading the children (with(children)), you are querying them a second time when ...
Oli's user avatar
  • 7,495
3 votes
Accepted

Tertiary Nav Items breaking Navigation when lightswitch set to zero

The issue is that the way you check whether the current entry has children is inconsistent. You're using the {% ifchildren %} tag, which will only check if the entry has children in the pages array, ...
MoritzLost's user avatar
  • 11.2k
2 votes

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

I know this post is old, but I'd like to share another technique I've been using for the past year. If you need a "hard-coded" navigation with no editing capabilities in the admin but still want ...
mateostabio's user avatar
2 votes

How to write a simple navigation?

This doesn't work for every project, but for brochure style sites I like having all the client-arrangeable pages in one Structure, with toggles on each entry to optionally show in primary or secondary ...
richardschaefer's user avatar
2 votes

How can I build a language switcher?

Here is my language switcher for Craft CMS 3: http://craftsnippets.com/articles/universal-language-switcher-for-craft-cms?s=s It works for Entries, categories and other elements with their own URLs ...
Piotr Pogorzelski's user avatar
2 votes

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

Sometimes you need the same parent/siblings menu from various POVs (parent or children). Here's what I came up with: {# Parent / Children subnavigation, either from the perspective of the parent or a ...
Urs's user avatar
  • 639
2 votes

Custom or complex nav tag

Here's an example for Craft 3, using macros. All in the same partial. First we set a macro for each distinctive level Then a macro that could be repeated infinitely Load structure with children Start ...
Urs's user avatar
  • 639
2 votes

Large, Complex Navigation

Carl's answer is very good and offers some quick performance wins. I just want to add a couple of things: First – I think its wrong to think of caching as "just masking the problem". In a situation ...
Mats Mikkel Rummelhoff's user avatar

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