Copied to clipboard

Flag this post as spam?

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


  • Lungelo Keswa 16 posts 97 karma points
    Nov 24, 2022 @ 14:42
    Lungelo Keswa
    0

    Breadcrumbs

    hi please help me out I'm trying to add bread crumbs on my Umbraco 10 website but i'm getting an error

    can some one please help me

    @{

          var selection =     CurrentPage.Ancestors();
    
    
      if (selection.Any())
       {
          foreach (var page in selection.OrderBy("Level"))
         {
            <a class="bread-link bread-home" href="@page.Url()">@page.Name</a> <span> > </span>
              }
              <span>@CurrentPage.Name</span>
            }
        }
    
  • Huw Reddick 1931 posts 6719 karma points MVP 2x c-trib
    Nov 24, 2022 @ 14:50
    Huw Reddick
    0

    what error are you getting?

  • Huw Reddick 1931 posts 6719 karma points MVP 2x c-trib
    Nov 24, 2022 @ 15:00
    Huw Reddick
    0

    I'm going to take a guess that your error relates to using page as your variable as I believe it is a reserved name (I had similar issues :) )

    This is my breadrumb code

    @{
        var CurrentPage = UmbracoContext.PublishedRequest.PublishedContent;
    
    }
    <ul class="forum-breadcrumb">
    
    @if (CurrentPage.Ancestors().Any())
    {
    
        foreach (var bpage in CurrentPage.Ancestors().OrderBy(x => x.Level))
        {
            if (!bpage.IsDocumentType("forumAuthHolder"))
            {
                <li class="d-none d-sm-block"><a href="@bpage.Url()">@bpage.Name</a>&nbsp;|&nbsp;</li>
            }
    
        }
    
        @* Display the current page as the last item in the list *@
        <li class="active d-none d-sm-block">@CurrentPage.Name</li>
    }
    
    </ul>
    
  • Lungelo Keswa 16 posts 97 karma points
    Nov 24, 2022 @ 15:32
    Lungelo Keswa
    0

    hi thanks man i'm getting an error now but the code I copied from your comment doesn't display anything on my page. Hmmmmmmmmmmmm maybe I'm adding it in the wrong place. Can you please walk me through on how you setted it up on your CMS

  • Huw Reddick 1931 posts 6719 karma points MVP 2x c-trib
    Nov 24, 2022 @ 16:23
    Huw Reddick
    100

    if you copied it exactly then it won't work as it has some code specific to my Forum templates, so you need to change this

    foreach (var bpage in CurrentPage.Ancestors().OrderBy(x => x.Level))
    {
        if (!bpage.IsDocumentType("forumAuthHolder"))
        {
            <li class="d-none d-sm-block"><a href="@bpage.Url()">@bpage.Name</a>&nbsp;|&nbsp;</li>
        }
    
    }
    

    To this

    foreach (var bpage in CurrentPage.Ancestors().OrderBy(x => x.Level))
    {
                    <li class="d-none d-sm-block"><a href="@bpage.Url()">@bpage.Name</a>&nbsp;|&nbsp;</li>
    
    }
    

    In my master template I just include it using

    @await Html.PartialAsync("_Breadcrumb")
    
  • Lungelo Keswa 16 posts 97 karma points
    Nov 25, 2022 @ 15:40
    Lungelo Keswa
    0

    sorry for the late reply man thank you so much for the code it worked.

  • Mike Allen 4 posts 74 karma points
    Nov 27, 2023 @ 21:46
    Mike Allen
    0

    Isn't this built into Unbraco or is there a module available ? When I used to use WP we just used the yoast plugin.

  • Huw Reddick 1931 posts 6719 karma points MVP 2x c-trib
    Nov 28, 2023 @ 06:40
    Huw Reddick
    0

    No it isn't built in.

  • Lee M. Childress 16 posts 96 karma points
    May 23, 2024 @ 14:55
    Lee M. Childress
    0

    Pardon my ignorance, as I am new to Umbraco (coming from Typo3). Love Umbraco, but there are a few things that are stumping me, one of which is breadcrumbs. I'm using the example code provided above by Huw on November 24 and it sorta kinda works.

    My page structure looks like this:

    Home

    -- Privacy Policy

    -- Terms Of Use

    When the Terms Of Use page is displayed, I get:

    Home -> Privacy Policy -> Terms Of Use

    It should read:

    Home -> Terms Of Use

    What am I doing wrong? Thank you in advance for any help.

  • Huw Reddick 1931 posts 6719 karma points MVP 2x c-trib
    May 23, 2024 @ 16:49
    Huw Reddick
    0

    Hi Lee,

    You are not doing anything wrong, the code is returning the ancestors as it should, if you want to not display the privacy policy then you probably need to do CurrentPage.Ancestors(1) which will only return the first level ancestors

  • Lee M. Childress 16 posts 96 karma points
    May 23, 2024 @ 17:23
    Lee M. Childress
    0

    So I updated my code to add the 1 as shown below, but still get the same results (Home -> Privacy Policy -> Terms of Use)

        @if (CurrentPage.Ancestors().Any())
    {
        foreach (var bpage in CurrentPage.Ancestors(1).OrderBy(x => x.Level))
        {
                                            <li><a href="@bpage.Url()">@bpage.Name</a></li>                                                                                <li class="active">Privacy Policy</li>
        }
    
        @* Display the current page as the last item in the list *@
        <li class="active">@CurrentPage.Name</li>
    }
    
  • Huw Reddick 1931 posts 6719 karma points MVP 2x c-trib
    May 23, 2024 @ 18:16
    Huw Reddick
    0

    Mmmnot sure tbh, but will have a little lay and see what I come up with.

  • Huw Reddick 1931 posts 6719 karma points MVP 2x c-trib
    May 23, 2024 @ 18:31
    Huw Reddick
    0

    This is a problem with your code, you are manually adding the Privacy policy link!

           @if (CurrentPage.Ancestors().Any())
        {
            foreach (var bpage in CurrentPage.Ancestors(1).OrderBy(x => x.Level))
            {
                 <li><a href="@bpage.Url()">@bpage.Name</a></li> 
    <li class="active">Privacy Policy</li>
            }
    
            @* Display the current page as the last item in the list *@
            <li class="active">@CurrentPage.Name</li>
        }
    
  • Lee M. Childress 16 posts 96 karma points
    May 23, 2024 @ 18:37
    Lee M. Childress
    0

    I promise you I'm not usually this dense ...

    So you are saying within the foreach loop bpage.name = "Privacy Policy"? If that is the case, how do I only get parent documents?

  • Huw Reddick 1931 posts 6719 karma points MVP 2x c-trib
    May 23, 2024 @ 19:19
    Huw Reddick
    0

    That's ok, we all have the odd brain freeze 🤣

    If you just want it's parent, then currentpage.parent should work

  • Lee M. Childress 16 posts 96 karma points
    May 23, 2024 @ 19:22
    Lee M. Childress
    0

    Let's say my tree looks like this:

    Home

    -- Legal

    -- -- Privacy Policy

    -- -- Terms Of Use

    How do I write the code so it would output:

    Home -> Legal -> Terms Of Use

    I just want the parents all the way up the tree

  • Huw Reddick 1931 posts 6719 karma points MVP 2x c-trib
    May 24, 2024 @ 15:09
    Huw Reddick
    0

    Hi, sorry been out of action today, dosed up on pain meds so brain is a bit foggy, I will provide a code sample for you first thing tomorrow 🙂

  • Lee M. Childress 16 posts 96 karma points
    May 24, 2024 @ 15:10
    Lee M. Childress
    0

    That would be awesome, but more importantly I hope you get feeling better!

  • Huw Reddick 1931 posts 6719 karma points MVP 2x c-trib
    May 25, 2024 @ 14:41
    Huw Reddick
    0

    Hi Lee,

    I think something like this should do what you want

    <ul>
        @{
            var CurrentPage = UmbracoContext.PublishedRequest.PublishedContent;
            List<IPublishedContent> parents = new List<IPublishedContent>();
            if (CurrentPage.Parent != null)
            {
                var thisparent = CurrentPage.Parent;
                while (thisparent != null)
                {
                    parents.Add(thisparent);
                    if (thisparent.Parent != null)
                    {
                        thisparent = thisparent.Parent;
                    }
    
                }
                // Reverse list items
                parents.Reverse();
                foreach (var parent in parents)
                {
                    <li><a href="@parent.Url()">@parent.Name</a></li>
                }
            }
            @* Display the current page as the last item in the list *@
            <li class="active">@CurrentPage.Name</li>
        }
    </ul>
    
  • Lee M. Childress 16 posts 96 karma points
    May 28, 2024 @ 12:39
    Lee M. Childress
    0

    Thank you for your time with this. I want you to know it is really appreciated. However, when using the code above, the page just spins and will not load. I'm shocked that with a system as robust as Umbruco it is so difficult to do a simple breadcrumb trail.

  • Huw Reddick 1931 posts 6719 karma points MVP 2x c-trib
    May 28, 2024 @ 12:49
    Huw Reddick
    0

    Can't see any reason it would do that. What ide are you using for your development? You should be able to stick in a breakpoint to see what it is doing.

    A have breadcrumbs on most of my sites without an issue.

  • Lee M. Childress 16 posts 96 karma points
    May 28, 2024 @ 12:54
    Lee M. Childress
    0

    Visual Studio Code

    My initial code works the best except that it is pulling EVERY page from current up instead of parents. Code below again just to review and see if you spot what I am doing wrong.

    @if (CurrentPage.Ancestors().Any())
    {
        foreach (var bpage in CurrentPage.Ancestors().OrderBy(x => x.Level))
        {
                                            <li><a href="@bpage.Url()">@bpage.Name</a></li>                                                                                <li class="active">Privacy Policy</li>
        }
    
        @* Display the current page as the last item in the list *@
        @* <li class="active">@CurrentPage.Name</li> *@
    }
    
  • Lee M. Childress 16 posts 96 karma points
    May 28, 2024 @ 18:07
    Lee M. Childress
    0

    Huw, I owe you such a HUGE apology for wasting your time. It turns out way off to the right off of my screen in my text editor was a hard coded li tag from my mockup. Apparently the above code worked. However, I did something and now I get an error which says CurrentPage is not in the current context. I just can't win!

    UPDATE: I was able to get CurrentPage functioning again. Now (because the saga must continue) I get the error bpage does not exist in the current context.

  • Lee M. Childress 16 posts 96 karma points
    May 28, 2024 @ 18:50
    Lee M. Childress
    0

    Now that you are all sick of me, I have resolved the issue. Was missing a @ before foreach.

    Seriously, thank you for all the time you put in assisting me.

  • Huw Reddick 1931 posts 6719 karma points MVP 2x c-trib
    May 28, 2024 @ 19:24
    Huw Reddick
    0

    No worries, sorry was a bit busy today to respond, glad you got it working

  • Maud Langaskens 2 posts 72 karma points
    May 31, 2024 @ 09:58
    Maud Langaskens
    0

    For future reference: you can create a breadcrumb very easily using a partial view snippet. Just go to Settings section in the backoffice and right-click the Partial Views folder. Choose Create and pick the breadcrumb partial. Save the partial and include it in your layout.

    https://docs.umbraco.com/umbraco-cms/fundamentals/design/partial-views#creating-a-partial-view-from-snippet

  • Huw Reddick 1931 posts 6719 karma points MVP 2x c-trib
    May 31, 2024 @ 10:24
    Huw Reddick
    0

    But may not suit everyone depends how your backoffice is set up

Please Sign in or register to post replies

Write your reply to:

Draft