Copied to clipboard

Flag this post as spam?

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


  • Utpal Maity 5 posts 35 karma points
    Mar 10, 2014 @ 12:23
    Utpal Maity
    0

    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>

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Mar 10, 2014 @ 17:08
    Ismail Mayat
    100

    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

  • Brian Reimer 30 posts 120 karma points
    Mar 10, 2014 @ 17:57
    Brian Reimer
    0

    Take a look at this:

    http://our.umbraco.org/projects/starter-kits/navit-mvc

    - it'll do the job for you :)

  • Utpal Maity 5 posts 35 karma points
    Mar 11, 2014 @ 06:44
    Utpal Maity
    0

    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.

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Mar 11, 2014 @ 10:30
    Ismail Mayat
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft