Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Render pages in different menus

Started by joh 6 years ago · 3 replies · 593 views
6 years ago

Hello folks,
I am doing a multi-lang project.
I need to separate the menu in two sections:
1) Main menu for the core navigation of the website
2) Footer navigation for stuff like legal, privacy, ...
All should be handable via pages

My questions: how can I tell a macro only to take some pages (e.g. marked as "footer") for the creation of a list of page links?

Spoiler: Cannot hardcode it: otherwise multilanguage will die.

6 years ago

So what I have found out so far...
I found a quite ok solution. I add every link that I want to appear in a specific menu with the following code:

TWIG
{% set c_page = page.find('/blog') %}
<li class="menu-item {% if c_page.active %}active{% endif %}">
<a href="{{c_page.url}}">{{c_page.menu}}</a>
</li>

This example adds the page in the folder "blog".

I am not a fan of it because:

  • It is not a proper function :-/
  • I have to go in code and cannot add pages in the admin
  • It has a bug with multi-language: when I switch languages, it renders the url out of the english path-name for the page. Thus I get URLs that are not the right ones for the languages as specified in the href-lang tags. Might confuse search engines.

Anyone some other solution in mind? 🙂

6 years ago

Hello,

You could add a specific field in your pages blueprint and render them in the menu depending on the value of that field.

It could be a taxonomy field, it could be a bolean "menuFooter : true" or even a select field.

I'm not sure if it's the best option, but it's what I would do.

I remember someone doing it differently, he had a "page" (a page in the admin) not indexed and not rendered in the menu, with the content being a list of links. And he included this page in his footer.
This way, you don't have to change a value in each pages, but simply modify that "page".

6 years ago

yes, that could work. I was too lazy doing that and hoped I was missing a feature :-D
It could even be a multi-select if you have several menus.
Thanks AmauryH

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 196 2 months ago
Themes & Styling · by Ian, 2 months ago
3 92 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 454 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 47 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 126 3 months ago