Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 231 posts 901 karma points c-trib
    Jun 16, 2020 @ 06:47
    Martin Rud
    0

    Previous and next links for nodes in a category structure

    Hi,

    I have a FAQ section with this structure:

    • FAQ overview
      • FAQ item 1
      • FAQ item 2
      • ...
      • FAQ item x

    For this I have this working code on the FAQ item template that creates next and prev links:

    @{
    IPublishedContent parent = Umbraco.AssignedContentItem.Parent;
    IEnumerable<IPublishedContent> children = parent.Children;
    List<int> childrenIds = children.Select(x => x.Id).ToList();
    int currentIndex = childrenIds.IndexOf(Umbraco.AssignedContentItem.Id);
    
    IPublishedContent prev = null;
    IPublishedContent next = null;
    
    if (currentIndex > 0)
    {
        prev = children.ElementAt(currentIndex - 1);
    }
    
    if (currentIndex < (children.Count() - 1))
    {
        next = children.ElementAt(currentIndex + 1);
    }
    }
    
    
    @if (prev != null)
    {
        <a class="prevnext prev" title="@prev.Name" href="@prev.Url">Prev</a>
    }
    
    @if (next != null)
    {
        <a class="prevnext next" title="@next.Name" href="@next.Url">Next</a>
    }
    

    But for this strucure I need to tweak the code. Any hints?

    • Event overview
      • Event category 1
        • Event item 1
        • Event item 2
        • ...
        • Event item x
      • Event category 2
        • Event item 1
        • Event item 2
        • ...
        • Event item x
      • ...
      • Event category x
        • Event item 1
        • Event item 2
        • ...
        • Event item x
  • Steve Morgan 1345 posts 4452 karma points c-trib
    Jun 16, 2020 @ 14:54
    Steve Morgan
    100

    Hi,

    You should be able to use similar logic.

    I'm assuming this is in a partial which is why you're using AssignedContentItem..

    Change

    IPublishedContent parent = Umbraco.AssignedContentItem.Parent;
    

    To: (updating the doc type alias to match yours)

    var eventOverview = Umbraco.AssignedContentItem.AncestorOrSelf("eventOverviewDocTypeAlias");
    

    And then

    IEnumerable<IPublishedContent> children = parent.Children;
    

    To (updating the doc type alias to match yours)

    var children = eventOverview.DescendantsOfType("eventItemDocTypeAlias");
    

    NOTE: you will have to change "eventOverviewDocTypeAlias" and "eventItemDocTypeAlias" to be the doc type aliases that match yours - this is the event landing page and the one for the individual the event items. You can get this from the Info tab, over over the text under "Document Type". It will usually be in camelCase, this is important.

    I hope this helps.

    Steve

  • Martin Rud 231 posts 901 karma points c-trib
    Jun 16, 2020 @ 17:47
    Martin Rud
    0

    Great! Thank you. That did the trick.

    Since I have only one document type of items to user it for that is structured in categories (the others are child nodes of the index page) I have changed the code to be generic for my setup:

    IPublishedContent root = null;
    if(Model.ContentType.Alias == "uge30_activity")
    {
        root = Umbraco.AssignedContentItem.AncestorOrSelf("uge30_activities_index");
    } else {
        root = Umbraco.AssignedContentItem.Parent;
    }
    var children = root.DescendantsOfType(Model.ContentType.Alias);
    
    List<int> childrenIds = children.Select(x => x.Id).ToList();
    int currentIndex = childrenIds.IndexOf(Umbraco.AssignedContentItem.Id);
    
    IPublishedContent prev = null;
    IPublishedContent next = null;
    
    if (currentIndex > 0)
    {
        prev = children.ElementAt(currentIndex - 1);
    }
    
    if (currentIndex < (children.Count() - 1))
    {
        next = children.ElementAt(currentIndex + 1);
    }
    

    The "uge30_activity" is the only one not directly put under the overview pages.

Please Sign in or register to post replies

Write your reply to:

Draft