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> | </li>
}
}
@* Display the current page as the last item in the list *@
<li class="active d-none d-sm-block">@CurrentPage.Name</li>
}
</ul>
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
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.
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
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>
}
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>
}
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>
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.
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.
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> *@
}
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.
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.
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
@{
what error are you getting?
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
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
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
To this
In my master template I just include it using
sorry for the late reply man thank you so much for the code it worked.
Isn't this built into Unbraco or is there a module available ? When I used to use WP we just used the yoast plugin.
No it isn't built in.
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.
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 ancestorsSo I updated my code to add the 1 as shown below, but still get the same results (Home -> Privacy Policy -> Terms of Use)
Mmmnot sure tbh, but will have a little lay and see what I come up with.
This is a problem with your code, you are manually adding the Privacy policy link!
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?
That's ok, we all have the odd brain freeze 🤣
If you just want it's parent, then currentpage.parent should work
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
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 🙂
That would be awesome, but more importantly I hope you get feeling better!
Hi Lee,
I think something like this should do what you want
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.
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.
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.
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.
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.
No worries, sorry was a bit busy today to respond, glad you got it working
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
But may not suit everyone depends how your backoffice is set up
is working on a reply...