开发者

Sitecore: How to display content from an items first child?

开发者 https://www.devze.com 2023-02-20 15:29 出处:网络
Say I have a content structure like: About Us Information Contact Us Blah When a user clicks About Us, how can I make it display the Information pag开发者_开发问答e?

Say I have a content structure like:

About Us
  Information
  Contact Us
  Blah 

When a user clicks About Us, how can I make it display the Information pag开发者_开发问答e?

This would be required on multiple site sections so hardcoding links / redirects won't work.

How can I make a page display the content from the first child, if exists?


I think it might be easier to answer if you describe what you are trying to do.

But if you are on the "About Us" page (ie. Sitecore.Context.Item is the "About Us" item).

You can check if the current item has children like this:

if (Sitecore.Context.Item.HasChildren) { /* do something */ }

You can access the children in the following way:

Sitecore.Context.Item.Children

This returns a ChildList object on which you could call GetArray() to get an Item[].

If you just blindly wanted the first item I guess you could call:

Sitecore.Context.Item.Children.ToArray()[0]; //arrays are zero-based, so the first item is 0 and not 1

But try to write a bit about what you are trying to do (ie make a menu or something).


Assuming you're using xslt and 'About Us' is the context item:

<sc:link select="item[1]" />

item will get all items below the context (current) item, and [1] will choose the first.


I have actually implemented this behavior for one of our bigger websites.

A working (works for me) way you can also do this is:

if (item.TemplateName.ToLower().ToString() == "main menu item")
{

    if (item.Children.Any())
    {
       url = GetItemUrl(item.Children.First<Item>());
    }
    else
    {
       url = GetItemUrl(item);
    }
}
else
{
     url = GetItemUrl(item);
}

In this case GetItemUrl is used to get the url, but I can imagine you using LinkManager.GetItemUrl(Item item);


You might want to add some template checking in here, but how about this:

if (Sitecore.Context.Item.HasChildren) {
  var children = Sitecore.Context.Item.GetChildren();
  Response.Redirect(LinkManager.GetItemUrl(children[0]))
}

This will see if the current page has children and if so will redirect to that page. I actually do this on a site for a law firm that has office locations. So when you go to the main locations page, it will redirect to the first office alphabetically.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号