Copied to clipboard

Flag this post as spam?

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


  • Sabin Regmi 13 posts 93 karma points
    Mar 08, 2016 @ 08:11
    Sabin Regmi
    0

    News Item: Find Next and Previous news item.

    Umbraco Content Structure

    Above image is a umbraco structure of my site. In newsDetail.cshtml view, i want to display Next and Previous news item below the current news detail. When I try Previous() and Next() method, it works only for current day item. But I want to display Once Upon a Time in a Galaxy Far Away item when click Next item in News1 detail page. Thanks in advance.

  • Martin Almström 33 posts 159 karma points
    Mar 08, 2016 @ 08:36
    Martin Almström
    1

    Hi Sabin,

    Don't know if this will work, but something like this maybe:

                CurrentPage.AncestorOrSelf("Blog").Descendants("BlogPost").First(x => x.Id.Equals(CurrentPage.Id)).Next();
    

    You'll have to change the documentTypeAliases to match yours.

    This will find the "Blog" container and get all descendants of "BlogPost". Then you'll need to find the current page in that list and then use Next or Previous.

    /Martin

  • Sabin Regmi 13 posts 93 karma points
    Mar 08, 2016 @ 09:35
    Sabin Regmi
    0

    Hi Martin, Thanks for reply. But it works for a same day only. If next news item is in next day or month (may be next year) then it does not work. Can you suggest any other idea??

    Regard:

    Sabin

  • Martin Almström 33 posts 159 karma points
    Mar 08, 2016 @ 10:29
    Martin Almström
    100

    Hi,

    Ok. The Next() command is the culprit. Maybe something like this:

    Find all the descendants of the blog container

    var allBlogPosts = CurrentPage.AncestorOrSelf("Blog").Descendants("BlogPost");
    

    Find the one that is CurrentPage

    var currentIndexInAllBlogPosts = allBlogPosts.FindIndex(x => x.Id.Equals(CurrentPage.Id));
    

    Then get the next or previous item in the list if CurrentPage is found:

       if (currentIndexInAllBlogPosts > -1){
          var nextItem = allBlogPosts[currentIndexInAllBlogPosts +1];
          var previousItem = allBlogPosts[currentIndexInAllBlogPosts -1];
       }
    

    Of course you need to check so that the index isnt out of bounds.

    Hope that helps.

    /M

  • Sabin Regmi 13 posts 93 karma points
    Mar 08, 2016 @ 11:35
    Sabin Regmi
    0

    Hi Martin,

    Lots of Thanks. It works. Yeah, I code to check the index out of bounds. Here is my code:

    var newsList = newsOverview.Descendants("NewsDetail").OrderByDescending(x => x.SortOrder).OrderByDescending(x => x.GetProperty("date").Value).ToList();
    var currentIndexInNewsList = newsList.FindIndex(x => x.Id.Equals(CurrentPage.Id));
    totalResults = newsList.Count();
    
    @if (currentIndexInNewsList > -1)
            {
    
            <div class="more-posts">
                @if (totalResults-1 > currentIndexInNewsList)
                {
                    var nextItem = newsList[currentIndexInNewsList + 1];
                    <div class="next-post">
                         <span><i class="fa fa-angle-right"></i></span>
                            <h3>Next Post <a href="@nextItem.Url">@nextItem.Name</a></h3>
                       </div>
    
                }
                @if (currentIndexInNewsList > 0)
                {
                    var previousItem = newsList[currentIndexInNewsList - 1]; 
                     <div class="line-sep1"></div>
                    <div class="prev-post">
                        <span><i class="fa fa-angle-left"></i></span>
                        <h3>Previous Post <a href="@previousItem.Url">@previousItem.Name</a></h3>
                    </div>
    
                }      
    }
    

    Front End: enter image description here

    Hope further help from you. Thanks Again.

    Cheers, Sabin

Please Sign in or register to post replies

Write your reply to:

Draft