Search code examples
umbracopartial-viewsumbraco7breadcrumbs

Breadcrumb changing field name


My breadcrumb consists of the following:

Website > 1st site > 2nd site.

I want to change "Website" to "Home" and this is my code:

@if (!Umbraco.AssignedContentItem.DocumentTypeAlias.Equals("Home"))
    {
        <div class="breadcrumb" itemprop="breadcrumb">
            @foreach (var level in Model.Content.Ancestors().Where("Visible").OrderBy("Level"))
            {
                if (Umbraco.AssignedContentItem.DocumentTypeAlias.Equals("Website"))
                {
                    <p>test</p><a class="breadcrumb" href="@level.Url">Home</a>
                }
                else
                {
                    <a class="breadcrumb" href="@level.Url">@level.Name</a>
                }
                <span>&gt;</span>
            }
            @CurrentPage.Name
        </div>
    }

The first .Equals makes sure the breadcrumb is hidden on the homepage as the documentalias of my homepage is "Home".

The second .Equal should change "Website" into "Home". "Website" is also my DocumentAlias.

I am using Umbraco version 7.7.2

Does anyone know why this is not working?


Solution

  • I recommend using Model.DocumentTypeAlias to check the document type of the current page.

    Also, it might be easier to check which level you're on rather than checking the current document type alias: Model.Level == 1 would check if you're at the root (the homepage).

    When you're doing if (Umbraco.AssignedContentItem.DocumentTypeAlias.Equals("Website")), I would imagine this is testing the document type of the current page, where as you want to test the current item in the foreach. Try:

    if (level.DocumentTypeAlias.Equals("Website"))
    {
        ...
    

    Full fixed example:

    @if (Model.Level != 1)
    {
        <div class="breadcrumb" itemprop="breadcrumb">
            @foreach (var level in Model.Content.Ancestors().Where("Visible").OrderBy("Level"))
            {
                if (level.DocumentTypeAlias.Equals("Website"))
                {
                    <p>test</p><a class="breadcrumb" href="@level.Url">Home</a>
                }
                else
                {
                    <a class="breadcrumb" href="@level.Url">@level.Name</a>
                }
                <span>&gt;</span>
            }
            @CurrentPage.Name
        </div>
    }
    

    Please note, if your page inherits from Umbraco.Web.Mvc.UmbracoTemplatePage rather than Umbraco.Web.Mvc.UmbracoViewPage, you will need to write Model.Content rather than just Model to access the current page content.