Copied to clipboard

Flag this post as spam?

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


  • pushpendu 23 posts 113 karma points
    Dec 30, 2014 @ 14:41
    pushpendu
    0

    problem creating nextpost and previous post button

    Hi,

    I need to display 3 posts and a button for displaying next or previous post. How can this be achieved?

    I am using foreach loop for displaying posts which reside inside news page post using Currentpage.Descendent(4).

    Thanks

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 30, 2014 @ 14:49
    Dennis Aaen
    0

    Hi pushpendu, 

    It is possible for you to share the code that you have so far, then I think it is much easier for people to help you, at least point you in the right direction.

    Looking forward to hear from you.

    /Dennis

  • pushpendu 23 posts 113 karma points
    Dec 31, 2014 @ 07:54
    pushpendu
    0

    HI Dennis,

    Thanks for your reply, Yes why not.. Here the code:

    <div class="row visible-xs">
    <div class="col-sm-12">

    //code for displaying 3 popular post:
    ---------------------------------------------------
    @{
    var count1 =1;
    foreach(var popular2 in CurrentPage.Descendants(4).Where("popularposts")){
    if(count1<4){
    <div class="media">
    <a class="pull-left" href="@popular2.Url"><img class="media-object" src="@popular2.pagebgimage" width="100" alt=""></a>
    <div class="media-body">
    <h4 class="media-heading"><a href="post-video.html">@popular2.Name</a></h4>
    <p class="post-date">@popular2.CreateDate</p>
    </div>
    </div>
    count1++;}}}
    </div></div>

    //Here is the HTML where I want insert code for displaying next and previous links
    -------------------------------------------------------------------------------------
    <div class="paging clearfix">
    <a class="btn pull-left" href="#"><i class="icon-arrow-left2 left"></i><span>Older</span><span class="hidden-xs"> Posts</span></a>
    <a class="btn pull-right" href="#"><span>Newer</span><span class="hidden-xs"> Posts</span><i class="icon-arrow-right2 right"></i></a>
    </div>

     

    Here is the screensort of what I want.

    Thanks

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 31, 2014 @ 11:50
    Dennis Aaen
    0

    Hi pushpendu,

    With this could you should be able to get to the previous or next post but, it will just go directly link to the post.

    //Here is the HTML where I want insert code for displaying next and  previous links
    -------------------------------------------------------------------------------------
    <div class="paging clearfix">
    @if (@CurrentPage.Previous() != null){
    <a class="btn pull-left" href="@CurrentPage.Previous().Url"><i class="icon-arrow-left2 left"></i><span>Older</span><span class="hidden-xs"> Posts</span></a>
    }
    @if (@CurrentPage.Next() != null){
    <a class="btn pull-right" href="@CurrentPage.Next().Url"><span>Newer</span><span class="hidden-xs"> Posts</span><i class="icon-arrow-right2 right"></i></a>
    }
    </div>

    Perhaps, you are more after some paging where when you click on the previuous and next you will get a page with another 3 news items.

    @inheritsUmbracoTemplatePage
    @{Layout="MasterTemplate.cshtml";}
    @{
            var pageSize =3;
            var page =1;int.TryParse(Request.QueryString["page"],out page);
            var items =Model.Content.Children().Where(x => x.IsDocumentType("BlogPost")).OrderByDescending(x => x.CreateDate);
            var totalPages =(int)Math.Ceiling((double)items.Count()/(double)pageSize);

            if(page > totalPages)
            {
                    page = totalPages;
            }
            else if(page <1)
            {
                    page =1;
            }
    }

    <div class="container">
            @foreach(var item in items.Skip((page -1)* pageSize).Take(pageSize))
            {
                    <div class="item">
                            <a href="@item.Url">@item.Name</a>
                    div>
            }

            @if(totalPages >1)
            {
                    <div class="pagination">
                            <ul>
                                    @if(page >1)
                                    {
                                            <li><a href="?page=@(page-1)">Prev</a>li>
                                    }
                                    @for(int p =1; p < totalPages +1; p++)
                                    {
                                            var active =(p == page)?" class=\"active\"":string.Empty;
                                            <li@(Html.Raw(active))>
                                                    <a href="?page=@p">@p</a>
                                           </li>
                                    }
                                    @if(page <totalPages)
                                    {
                                            <li><a href="?page=@(page+1)">Next</a>li>
                                    }
                            </ul>
                    </div>
            }
    </div>

    If you are using the paging script remember to change it so it fit your document types and so on.

    Hope this helps, or at least could be a good starting point

    /Dennis

     

Please Sign in or register to post replies

Write your reply to:

Draft