Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Morten 105 posts 344 karma points
    Mar 09, 2017 @ 08:26
    Morten
    0

    Cultures and navigation

    Hello!

    I have a navigation bar, which has some links in it. I am using cultures (Danish and English), and I would like to generate my navigation links as much as possible.

    A problem I have is: I don't want to loop over each page, as I don't want some of the pages to appear there.

    I want to set an class="active" on the currently active page (or sub page). So if I have a news page, it should make it active. If I read a news article, it should also be active. The URL is /news/ and also /news/news-article-1 so the article is indeed in the correct spot.

    How would I achieve something like this?

    Requirements:

    • No foreach loop (unless I can tell it specifically which ones I don't want)
    • Set an class="active" on the active page
    • Be able to insert my own menu item (like a link to an external page)

    Thanks!

  • Justyna 14 posts 146 karma points
    Mar 09, 2017 @ 10:40
    Justyna
    0

    Hi Morten,

    If you add a property to your document types called umbracoNaviHide of type True/False and then check the box on the pages you don't want to display in navigation you can then do a foreach

    @foreach (var item in CurrentPage.Site().Children.Where("Visible"))
        {
            <li class="menu__item @(item.IsAncestorOrSelf(CurrentPage) ? "active" : null)">
                <a href="@item.Url" class="menu__link"><span class="menu__helper">@item.Name</span></a>
            </li>
        }`
    

    Where("Visible") will then only select the items that have umbracoNaviHide set to false.

    This bit @(item.IsAncestorOrSelf(CurrentPage) ? "active" : null) is what I normally do to add the active class.

  • Morten 105 posts 344 karma points
    Mar 09, 2017 @ 11:39
    Morten
    0

    Hi!

    Thanks, that could work indeed! Now I just have another question, which I hope you can help me with, because I think my page structure is messed up. In order to make your solution work, this problem needs to be solved:

    When I do this, I have three links shown: News - People - Services

    However, I really want to have: References - Services - News - Contact

    I currently have a Master content item, which has three subitems, which has my items in it. It goes something like this:

    I like this layout because I can create news under news, people under people, and services under services. Basically like models. However, I do not want my References page to have any sub items, because References does not have a model (it's just a static page).

    Now here are my Document Types and Templates:

    I think this is where I got it wrong, because when I go to create a new item, I select ListNews, ListPeople, or ListServices. These basically have the properties I need in order to create an item.

    Do you see where I'm going at? Here's my issue: When I go to /news/ it works fine. Model.Content.Url is /news/. If I go to /references/ it shows me an empty page, but the Model.Content.Url is /.

    Can you tell me what I am doing wrong? I think I got this wrong from the beginning (watching old tutorials perhaps).

    Thanks a bunch!!

  • Justyna 14 posts 146 karma points
    Mar 09, 2017 @ 13:48
    Justyna
    0

    Where on the content tree is the References page? Can you also provide a wider snippet of code that is not working the way you hoped?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies