54

You are probably not using this within a craft.entries loop. Position it inside a loop or set custom parameters for your prev and next elements: {# Set parameters for prev/next elements list #} {% set params = craft.entries.section('blog').order('title asc') %} {# Get the prev/next elements #} {% set prevEntry = entry.getPrev(params) %} {% set nextEntry = ...


32

Tip: To keep it short and reusable I would recommend that you wrap your preferred "check-method" in a Twig macro, I like Anna's version the most: _macros.html: {% macro is_active(segment) %}{% if craft.request.firstSegment == segment %}active{% endif %}{% endmacro %} layout.html: {% import "_macros.html" as macros %} <a href="{{ url('news') }}" class=...


32

This isn't documented, so it's subject to change: When you set a Single to be the homepage, its slug is automatically set to __home__. If you want your homepage to be part of a Structure, rather than a separate Single, you can set the slug of a Structure entry to __home__, which makes it act as the site's homepage and gives it that little Home icon in ...


30

Assuming you use a structure called pages to create your navigation and the entry variable represents the currently viewed entry, you can output your navigation and active state as follows: <ul> {% nav page in craft.entries.section('pages') %} <li class="{{ page.id == entry.id ? 'active' }}"> {{ page.link }} {% ifchildren %} ...


29

I am building my first multi-language site with Craft as well. This is from a thread over at Google+, so I didn't come up with this myself but it works very well: {% block locale__switch %} {# Loop through all of the site locales, except the current one #} {% set otherLocales = craft.i18n.getSiteLocaleIds()|without(craft.locale) %} {% for ...


19

You can set the prev.url at the top of the template, for example: {% set criteria = {section: 'blog', order: 'postDate'} %} {% set prev = entry.getPrev(criteria) %} Add a link if prev is available: {% if prev %} <a href="{{ prev.url }}">{{ prev.title }}</a> {% endif %}


18

If the {% nav %} tag doesn't give you enough control over the output, you can always replicate the behavior with custom loops and the getChildren() method. Just nest them as far as your navigation hierarchy possibly gets. Edit: Here an example code on how you'd dynamically call getChildren() on each top-level entry of your structure. If you need to go ...


18

Yes, you can! The first thing you need to do is to add the entire cached content into a single variable: {% set navigation %} {% cache globally for 3 years %} {# YOUR CACHED CONTENT #} {% endcache %} {% endset %} Then you need to add some dummy placeholder tags on your <li> tag and your <a> tag which then can be used by |...


17

My solution is to setup a dedicated "Menu" section and use an entries field type for the links. This allows you to pull together entries from multiple sections into a single menu. My normal setup is to have to custom fields relatedEntry - Entries field allowing you to select any other entry customURL - text field for custom url input The idea being that ...


17

A recursive macro will help you get around the limitations of ifchildren (which P&T will hopefully resolve soon), as follows: {% set mainEntry = craft.entries.slug(craft.request.getSegment(1)).first() %} {% set subnav = craft.entries.descendantOf(mainEntry).level(2) %} {% macro recursiveChildren(page) %} {% if page.hasDescendants and page.slug == ...


15

This is because you loop through all but the current locales. Remove the without filter to loop them all. {% set otherLocales = craft.i18n.getSiteLocaleIds()|without(craft.locale) %} . Add "current" class: To assign the current locale's link a class of "current", compare the looped locale to the current locale and set a variable that you can later test ...


14

I have changed the above code a little bit because it causes an error when I have for example a website with three active locales and an entry with only one or two active locales. The error is: Impossible to access an attribute ("locale") on a NULL variable ("") The expanded if-statement looks now like this: {% if localeEntry.locale is defined and ...


14

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 methods are very resource-intensive. You can mitigate with the cache tag, but ideally should strive for simplicity where possible... nav approaches Completely ...


14

craft.request is useful for setting an "active" class based on URL segments. If your URL was: domain.com/sale/products/shirts You could write conditionals like the following to target each segment: {% if craft.request.firstSegment == "sale" %}class="active"{% endif %} {% if craft.request.segment(2) == "products" %}class="active"{% endif %} {% if craft....


14

The "this is for the homepage" button available on single sections does two things for you, when you navigate to the home page's URL: it populates the entry variable with the single's entry model it loads the template spezified in the single's CP settings . Actually nothing you can't do manually with your home page's index.html: {# Replace the default `...


13

Got it! {% if craft.request.segments %} <nav class="breadcrubs"> <div class="container"> <div class="row"> <ol class="breadcrumb"> <li><a href="{{ siteUrl }}">Home</a></li> {% for crumb in entry.getAncestors() %} <li>{{ crumb....


12

Barrel Strength also have a URL segment based plugin called Sprout Active, with docs/examples here.


12

Structure for "sorting-only" I haven't tried the approach that you are thinking of yet, but your concerns regarding the query count are not that unfounded. You would need to use the relatedTo() parameter with the ElementCriteriaModel you create on your pages (to get the single's content) on your navigation include (even if you only want to get the entries ...


11

If I'm understanding you correctly, you want users when they visit your site to have a secondary menu that has links to various points in the article. That would actually be very easy to do in Craft. It's also possible for you to paginate a page, but that would be far more in-depth. For skip links, you would need to run over the matrix twice. You will also ...


10

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() %} {% for locale in locales %} {% set entryURL = craft.config.siteUrl[locale.id] %} {% if entry is defined %} {% set ...


10

In your template you can set a var in twig: {% set active = 'blog' %} And then in your navigation you can run an if statement to see what the var is <nav> <a {% if active = 'homepage' %}class="active"{% endif %}> Home </a> <a {% if active = 'blog' %} class="active"{% endif %}> Blog </a> </nav>


9

Here's yet another way you can do it (in some circumstances) and it's nice and DRY: Craft 2 {% if craft.request.getPath() == entry.uri %} Craft 3 {% if craft.app.request.pathInfo == entry.uri %}


9

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 separate template to include in each page: {% if entry is not defined %} {% set entry = null %} {% endif %} <ul> <li><a href="{{ craft....


9

I have one main structure "pages" and got rid of all single sections. I also gave each channel's index page an entry in the structure (good for URL translation). Then I added some entry types to it (you can also link templates to them) and created my site navigation like in my example code below. This allows you to translate the slug of the index pages in ...


9

You'll want to make use of the level property of your entry... {% if entry.level == 1 %} <!-- Top Level Links --> {% else %} <!-- Child Links --> {% endif %}


8

The Methods section on the EntryModel documentation page has a lot of useful things for working with navigation for Structure sections. You can use hasDescendants(): {% if entry.hasDescendants() %}class="has-dropdown"{% endif %}


8

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.slug={{ entry.slug }} DEBUG: menu.slug={{ menu.slug }} <li class="{{ menu.slug == entry.slug ? 'active' }}"> <a href="{{ ...


7

If you have more complex menus in the future, you should take a look at the Nav-Tag. . Getting singles: There isn't an easy way to get singles (yet). But you can use the same method which is used in crafts backend: {% set allSections = craft.sections.getAllSections() %} {% set singleSections = [] %} {% for section in allSections %} {% if section....


7

In some cases I use a pre-defined (very simple) nav that I don't need to be driven from the CMS yet I want to share it between the header / footer. In these cases I'll set an array in my base layout template so I can use it throughout. In "_partials/layout.twig" template: {% set nav = [ {'title':'Home', 'link':siteUrl, 'class':'homepage'}, {'title'...


7

Another way to achieve this: {% for entry in entries %} {% set active = craft.request.path ~ '/' matches "|^#{entry.uri}/|" %} <a href="{{ entry.url }}" class="{{ active ? 'is-active' : '' }}">{{ entry.title }}</a> {% endfor %} This accounts for even when a navigation item might not be on the top level, but you want every ...


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