Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hey :)
I've been trying to make a navigation menu for the first time.
I've most of it working, but can't figure out how to make the frontpage get the "current" class when that page is active.
Here is what i've for now:
@inherits Umbraco.Web.Macros.PartialViewMacroPage @{ var selection = CurrentPage.Site().Children.Where("Visible"); } <ul> <li><a href="/">Home</a></li> @foreach (var item in selection) { <li class="@(item.IsAncestorOrSelf(CurrentPage) ? "current" : null)"> <a href="@item.Url">@item.Name</a> @if (item.Children.Where("Visible").Count() > 0) { <ul class="dropdown"> @foreach (var subpage in item.Children.Where("Visible")) { <li><a href="@subpage.Url">@subpage.Name</a></li> } </ul> } </li> } </ul>
Thanks in advance :)
Hi Mikkel,
What do you mean with front page ? Your homepage ?
Dave
Hey Dave :)
Yes that's what i mean
This example should work. You only need to replace "HomePageDocTypeAlias" with the actual alias of your homepage doctype :
@inherits Umbraco.Web.Macros.PartialViewMacroPage @{ var selection = Model.Content.AncestorOrSelf(1).Children().Where(x => x.IsVisible()).ToList(); } <ul> <li class="@(Model.Content.DocumentTypeAlias == "HomePageDocTypeAlias" ? "current" : null)"><a href="/">Home</a></li> @foreach (var item in selection) { <li class="@(item.IsAncestorOrSelf(Model.Content) ? "current" : null)"> <a href="@item.Url">@item.Name</a> @{ var children = item.Children.Where(x => x.IsVisible()).ToList(); } @if (children.Any()) { <ul class="dropdown"> @foreach (var subpage in children) { <li><a href="@subpage.Url">@subpage.Name</a></li> } </ul> } </li> } </ul>
I also took the liberty to refactor it to use strongly typed content (Model.Content) access instead of the use of dynamics (CurrentPage)
To have a good understanding between the difference read this article I wrote : http://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/
Hey again :D
Thank you! Works perfectly, going to dig into your article asap :)
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
if currentpage is frontpage add current class
Hey :)
I've been trying to make a navigation menu for the first time.
I've most of it working, but can't figure out how to make the frontpage get the "current" class when that page is active.
Here is what i've for now:
Thanks in advance :)
Hi Mikkel,
What do you mean with front page ? Your homepage ?
Dave
Hey Dave :)
Yes that's what i mean
Hi Mikkel,
This example should work. You only need to replace "HomePageDocTypeAlias" with the actual alias of your homepage doctype :
I also took the liberty to refactor it to use strongly typed content (Model.Content) access instead of the use of dynamics (CurrentPage)
To have a good understanding between the difference read this article I wrote : http://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/
Dave
Hey again :D
Thank you! Works perfectly, going to dig into your article asap :)
is working on a reply...