webjaved
07/24/2023, 10:37 AMHooky
07/24/2023, 11:07 AMwebjaved
07/24/2023, 11:10 AMHooky
07/24/2023, 11:18 AM<ul id="menu-main-menu" class="menu">
@foreach (var item in selection)
{
<li>
<a href="@item.Url" title="@item.Name">@item.Name</a>
@if(item.Children().Any())
{
<ul class="submenu">
foreach(var subitem in item.Children())
{
<li>
<a href="@subitem.Url" title="@subitem.Name">@subitem.Name</a>
</li>
}
</ul>
}
</li>
}
</ul>
Hooky
07/24/2023, 11:19 AMitem.Children()
should give you the 'childrens children' of your home nodeHooky
07/24/2023, 11:20 AMwebjaved
07/24/2023, 11:21 AMerror CS0103: The name 'subitem' does not exist in the current context'
Hooky
07/24/2023, 11:22 AMwebjaved
07/24/2023, 11:23 AMHooky
07/24/2023, 11:24 AMwebjaved
07/24/2023, 11:29 AMwebjaved
07/24/2023, 11:30 AMwebjaved
07/24/2023, 11:36 AMvar selection = Umbraco.ContentAtRoot().FirstOrDefault().Children().Where(x => x.IsVisible());
System.NullReferenceException: 'Object reference not set to an instance of an object.'Hooky
07/24/2023, 11:37 AM<ul>
@foreach (var item in selection)
{
<li>
<a href="@item.Url" title="@item.Name">@item.Name</a>
@if (item.Children().Any())
{
<ul class="submenu">
@foreach(var subitem in item.Children())
{
<li>
<a href="@subitem.Url" title="@subitem.Name">@subitem.Name</a>
</li>
}
</ul>
}
</li>
}
</ul>
Might also depend what version of Umbraco you're using. Either way, the .Children()
method of your items in your loop should output what you need 🙂webjaved
07/24/2023, 11:38 AMHooky
07/24/2023, 11:39 AMHooky
07/24/2023, 11:42 AMwebjaved
07/24/2023, 11:43 AMHooky
07/24/2023, 11:45 AMwebjaved
07/24/2023, 11:46 AMHooky
07/24/2023, 11:46 AMHooky
07/24/2023, 11:46 AMwebjaved
07/24/2023, 11:49 AMwebjaved
07/24/2023, 11:53 AMHooky
07/24/2023, 11:57 AMwebjaved
07/24/2023, 12:03 PM<li class="@if(item.Children().Any()){ menu-item-has-children }">
System.Web.HttpCompileException: 'D:\sites\PrincipleNetworks.Web\Views\Partials\navigation.cshtml(13): error CS1002: ; expected'Hooky
07/24/2023, 12:08 PM@foreach (var item in selection)
{
var hasChildren = item.Children.Any();
<li class="@(hasChildren == true ? "someClass" : null)">
You could do something like that (I've made 'hasChildren' a variable so that it's a little bit easier to read)Hooky
07/24/2023, 12:09 PMwebjaved
07/24/2023, 12:12 PMSystem.Web.HttpCompileException: 'D:\sites\PrincipleNetworks.Web\Views\Partials\navigation.cshtml(5): error CS1002: ; expected'
Hooky
07/24/2023, 12:13 PMwebjaved
07/24/2023, 12:15 PMHooky
07/24/2023, 12:15 PMwebjaved
07/24/2023, 12:16 PMMike Chambers
07/24/2023, 1:45 PMcsharp
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
@using Umbraco.Cms.Core
@using Umbraco.Cms.Core.Models.PublishedContent
@using Umbraco.Cms.Core.Routing
@using Umbraco.Extensions
@inject IPublishedValueFallback PublishedValueFallback
@inject IPublishedUrlProvider PublishedUrlProvider
@*
This snippet makes a list of links of all visible pages of the site, as nested unordered HTML lists.
How it works:
- It uses a local method called Traverse() to select and display the markup and links.
*@
@{ var selection = Model.Root(); }
<div class="sitemap">
@* Render the sitemap by passing the root node to the traverse method, below *@
@{ Traverse(selection); }
</div>
@* Helper method to traverse through all descendants *@
@{
void Traverse(IPublishedContent? node)
{
//Update the level to reflect how deep you want the sitemap to go
const int maxLevelForSitemap = 4;
@* Select visible children *@
var selection = node?.Children.Where(x => x.IsVisible(PublishedValueFallback) && x.Level <= maxLevelForSitemap).ToArray();
@* If any items are returned, render a list *@
if (selection?.Length > 0)
{
<ul>
@foreach (var item in selection)
{
<li class="level-@item.Level">
<a href="@item.Url(PublishedUrlProvider)">@item.Name</a>
@* Run the traverse method again for any child pages *@
@{ Traverse(item); }
</li>
}
</ul>
}
}
}
webjaved
07/24/2023, 8:49 PM