问题
I just wanna ask if there is someone here that have already made a breadcrumbs in Sitecore. I'm currently doing a Sitecore 8 MVC project that needs to have a breadcrumbs.
Currently I saw this website http://blog.ryanbailey.co.nz/2015/05/breadcrumbs-for-pages-in-sitecore.html. But It doesn't work for me yet because I don't know what to reference.
I just need to know how to get every item in the path of my current page I can handle it already.
Thanks
回答1:
Something like this should do it:
public ICollection<Item> GetBreadcrumbs(Item current, SiteContext site)
{
Item homeItem = site.StartItem;
List<Item> breadcrumbs = new List<Item>();
while (current != null)
{
// You may want to add additional logic to opt in/out of
// the breadcrumb based on a template/field
breadcrumbs.Add(current);
if (current == homeItem)
break;
current = current.Parent;
}
breadcrumbs.Reverse();
return breadcrumbs;
}
And then:
var breadcrumbs = GetBreadcrumbs(Context.Item, Context.Site);
回答2:
You can take the current item and then take all the ancestors of it.
var ancestors = currentItem.Axes.GetAncestors().ToList();
Then you can get home item and filter the ancestors to remove sitecore and content nodes.
ancestors = ancestors.SkipWhile(i => i.ID != home.Id.ToID()).ToList();
回答3:
public void GetBreadcrumbs(Item ParentItem)
{
List<BredCrumbDetails> lstBreadCrumbs = new List<BredCrumbDetails>();
string strcurrenttitle = ParentItem.Name;
Item currentitem = ParentItem;
int i = 0;
while (currentitem != null)
{
var ItemTemplateid = currentitem.TemplateID.ToString();
var FolderTemplateId = "{}";
if (ItemTemplateid != FolderTemplateId) //here we are removing folders
{
BredCrumbDetails bcDetails = new BredCrumbDetails();
if (i == 0)
{
bcDetails.BCPageLink = null;
bcDetails.Title = currentitem.Name;
bcDetails.IsVisible = true;
lstBreadCrumbs.Add(bcDetails);
}
else
{
bcDetails.BCPageLink = currentitem.Paths.FullPath;
bcDetails.Title = currentitem.Name;
bcDetails.IsVisible = true;
lstBreadCrumbs.Add(bcDetails);
}
i++;
if (currentitem.Name == ("Home"))
{
break;
}
currentitem = currentitem.Parent;
}
else
{
i++;
currentitem = currentitem.Parent;
}
}
lstBreadCrumbs.Reverse();
rptCrumbs.DataSource = lstBreadCrumbs;
rptCrumbs.DataBind();
}
来源:https://stackoverflow.com/questions/33053030/implementing-breadcrumbs-on-sitecore