Need help on dynamically creating navigation links based on content created in Umbraco
Hi All,
i am very much new in Umbraco CMS and started coding in it recently. My page structure is like this.
And now I have written the code like this to generate the dynamic navigation based on the code I have found in Umbraco forums. But in output it is giving only the 'Home' as the link not other links.The reason is that I have written it like this { var homeNode = Model.AncestorOrSelf(1); } and it is only returning home page not other pages. i have tried using this also Model.AncestorOrSelf(). But it is giving same result.
<nav>
<ul>
@{ var homeNode = Model.AncestorOrSelf(1); }
<li><a href="@homeNode.Url" class="@Library.If(homeNode.Id == Model.Id, "selected", "")">@homeNode.Name</a></li>
@foreach (var page in Model.Children.Where("Visible"))
{
var isSelected = false;
if (Model.Id == page.Id || (Model.Parent != null && Model.Parent.Id == page.Id && Model.NodeTypeAlias != "Textpage"))
{
isSelected = true;
}
<li>
<a href="@page.Url" class="@Library.If(isSelected, "selected", "")">@page.Name</a>
<!-- If the page has child nodes (2nd level) that are visible and docTypeAlias is Textpage (textpages) -->
@if (page.Textpages.Where("Visible").Count() > 0)
{
<ul>
@foreach (var childPage in page.Children.Where("Visible"))
{
<li><a href="@childPage.Url" class="@Library.If(childPage.Id == Model.Id, "selected", "")">@childPage.Name</a></li>
}
</ul>
}
</li>
}
</ul>
</nav>
Any reason why you would want to set up page tree like that? Typically you have at top level home pages so for multi lingual sites:
Home - EN
Home - FR
Home - ES
then under that all the child pages. Another scenario is you have again home then all the child pages and at top level other pages not for viewing eg Settings so
Home
Settings
In settings you have things like node ids for specific pages like news home page id so then you can use that in macros etc. Also you have settings like facebook or twitter urls.
Need help on dynamically creating navigation links based on content created in Umbraco
Hi All,
i am very much new in Umbraco CMS and started coding in it recently. My page structure is like this.
And now I have written the code like this to generate the dynamic navigation based on the code I have found in Umbraco forums. But in output it is giving only the 'Home' as the link not other links.The reason is that I have written it like this { var homeNode = Model.AncestorOrSelf(1); } and it is only returning home page not other pages. i have tried using this also Model.AncestorOrSelf(). But it is giving same result.
Utpal,
Its looks as though the pages other than home are at same level as home should they not be children of Home?
Regards
Ismail
Take a look at this:
http://our.umbraco.org/projects/starter-kits/navit-mvc
- it'll do the job for you :)
Yes , if i rearrange my page hierarchy like that it will work . But if I arrange the code like that then what will be the code.Please suggest.
Utpal,
Any reason why you would want to set up page tree like that? Typically you have at top level home pages so for multi lingual sites:
Home - EN Home - FR Home - ES
then under that all the child pages. Another scenario is you have again home then all the child pages and at top level other pages not for viewing eg Settings so
Home Settings
In settings you have things like node ids for specific pages like news home page id so then you can use that in macros etc. Also you have settings like facebook or twitter urls.
Regards
Ismail
is working on a reply...