Copied to clipboard

Flag this post as spam?

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


  • Martin 278 posts 662 karma points
    Mar 23, 2015 @ 11:24
    Martin
    0

    Navigation - Hide document types from nav

    Hi,

    Im looking for some help with my navigation partial.

    I have a multiple nodes with the document type "services" that should not appear in the navigation.

    Any help on how I should hide theses from the navigation would be grateful.

    Thanks

    Martin

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{ var home = CurrentPage.Site(); }
    
    @if (home.Children.Any())
    {
        @* Get the first page in the children *@
        var naviLevel = home.Children.First().Level;
    
    <nav class="nav">
        @* Add in level for a CSS hook *@
        <ul id="cd-primary-nav" class="level-@naviLevel">            
            @* For each child page under the home node *@
            @foreach (var childPage in home.Children.Where("Visible"))
            {   
                if (childPage.Children.Any())
                {                    
                    <li class="has-children @(childPage.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">
    
    
                        <a href="@childPage.Url">@childPage.Name</a>
    
                        @childPages(childPage.Children)
    
                    </li>
                } 
    
                else
                {
                    <li class="@(childPage.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">
                        <a href="@childPage.Url">@childPage.Name</a>
                    </li>
                } 
    
            }
        </ul>
    </nav>
    }
    
    
    
    
    @helper childPages(dynamic pages)
    {
        @* Ensure that we have a collection of pages *@
        if (pages.Any())
        {
            @* Get the first page in pages and get the level *@
            var naviLevel = pages.First().Level;
    
            @* Add in level for a CSS hook *@
            <ul class="sublevel level-@(naviLevel) ">
    
    
    
                @foreach (var page in pages)
                {
                    <li>
                        <a class="item" href="@page.Url">
                            <p>@page.Name</p>
                        </a>
    
                        @* if the current page has any children *@
                        @if (page.Children.Any())
                        {                        
                            @* Call our helper to display the children *@
                            @childPages(page.Children)
                        }
                    </li>
                }
            </ul>
        }
    }
    
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 23, 2015 @ 11:55
    Dennis Aaen
    0

    Hi Martin,

    On your document type for the services pages. Just add a new property with the alias of umbracoNaviHide, and set the type to be true/false, this should do it.

    So when it´s checked, the pages with the propperty is checked, the page should not appear in the navigation.

    Hope this helps,

    /Dennis

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 23, 2015 @ 12:10
    Dennis Aaen
    100

    Hi Martin

    Or if you want to do programmably it can be done like this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{ var home = CurrentPage.Site(); }

    @if (home.Children.Any())
    {
        @* Get the first page in the children *@
        var naviLevel = home.Children.First().Level;

    <nav class="nav">
        @* Add in level for a CSS hook *@
        <ul id="cd-primary-nav" class="level-@naviLevel">           
            @* For each child page under the home node *@
            @foreach (var childPage in home.Children.Where("Visible"))
            {  
                if (childPage.Children.Any())
                {                   
                    <li class="has-children @(childPage.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">


                        <a href="@childPage.Url">@childPage.Name</a>

                        @childPages(childPage.Children)

                    </li>
                }

                else
                {
                    <li class="@(childPage.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">
                        <a href="@childPage.Url">@childPage.Name</a>
                    </li>
                }

            }
        </ul>
    </nav>
    }




    @helper childPages(dynamic pages)
    {
        @* Ensure that we have a collection of pages *@
        if (pages.Any())
        {
            @* Get the first page in pages and get the level *@
            var naviLevel = pages.First().Level;

            @* Add in level for a CSS hook *@
            <ul class="sublevel level-@(naviLevel) ">



                 @foreach (var page in pages.Where("Visible").Where("DocumentTypeAlias != @0","Service")
                {
                    <li>
                        <a class="item" href="@page.Url">
                            <p>@page.Name</p>
                        </a>

                        @* if the current page has any children *@
                        @if (page.Children.Any())
                        {                       
                            @* Call our helper to display the children *@
                            @childPages(page.Children)
                        }
                    </li>
                }
            </ul>
        }
    }

    Remember to change the bold linie, so it match your document type alias for the service pages.

    Hope this helps, if you have other questions don't hesitate to ask again

    /Dennis

  • Martin 278 posts 662 karma points
    Mar 23, 2015 @ 12:11
    Martin
    0

    Hi Dennis, Thanks, yeah I have that set up for one off pages. Ideally I would need it to automatically hide any page with a "services" document type. I could leave it with umbracNaviHide, but content editors will forget to hide the pages.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 23, 2015 @ 12:14
    Dennis Aaen
    0

    Hi Martin,

    Try to see my comment above your latest. There is how you can do it automatically. If it gives you any problems feel free to ask again, and I would try to help you out.

    /Dennis

  • Martin 278 posts 662 karma points
    Mar 23, 2015 @ 12:52
    Martin
    0

    Hey Dennis, sorry I posted at the same time.

    Thanks, it works great.

    One last question, if I had multiple document types that I wanted to exclude, how would I tackle that?

    Thanks again.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 23, 2015 @ 12:55
    Dennis Aaen
    0

    Hi Martin,

    That´s no problem, you can exclude multiple document types using the following syntax,

    @foreach(var page in pages.Where("Visible").Where("DocumentTypeAlias != @0 || DocumentTypeAlias != @1","Service","DocumentTypeAlias2"){ 
    ...
    }

    Hope this helps,

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft