Copied to clipboard

Flag this post as spam?

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


  • Milly 30 posts 130 karma points
    Jun 06, 2017 @ 09:32
    Milly
    0

    Pagination with Categories

    Hi all, I have a basic blog set up with paging and category/archive navigation. The paging currently works for All blog posts, but not for within categories. Can anyone advise me how I'd do this? My current code:

        @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        var rootNode = Model.Content.AncestorOrSelf(1);
        var blog = rootNode.Children.First(x => x.DocumentTypeAlias == "blogMaster");
        var blogCategoriesFolder = blog.Children.First(x => x.DocumentTypeAlias == "blogCategoriesFolder");
        var blogPostsFolder = blog.Children.First(x => x.DocumentTypeAlias == "blogPostsFolder");
        var blogPosts = blogPostsFolder.Children().Where(x => x.IsDocumentType("blogPost")).OrderByDescending(x => x.CreateDate);
    
        // PAGINATION
        var pageSize = 3;
        var page = 1; int.TryParse(Request.QueryString["page"], out page);
        var totalPages = (int)Math.Ceiling((double)blogPosts.Count() / (double)pageSize);
        string pageURL = blog.Url + "?page=";
    }
    
    @if (blogPosts.Count() > 0)
    {
        if (page > totalPages)
        {
            page = totalPages;
        }
        if (page < 1)
        {
            page = 1;
        }
    
        <div id="categories">
            @foreach (var blogPost in blogPosts.Skip((page - 1) * pageSize).Take(pageSize))
            {
                if (Request.Url.ToString().Contains("?category"))
                {
                    var blogPostCategoryList = blogPost.GetPropertyValue<IEnumerable<IPublishedContent>>("postCategories");
    
                    foreach (var blogCategory in blogPostCategoryList)
                    {
                        string currentURL = Request.Url.ToString();
                        string blogCategoryURL = blogCategory.Id.ToString();
    
                        if (currentURL.Contains(blogCategoryURL))
                        {
                            <div class="item">
                                <a href="@blogPost.Url">@blogPost.Name</a>
                                <p>@blogPost.GetPropertyValue("postDate").ToString()</p>
                            </div>
                        }
                    }
                }
                else
                {
                    <div class="item">
                        <a href="@blogPost.Url">@blogPost.Name</a>
                        <p>@blogPost.GetPropertyValue("postDate").ToString()</p>
                    </div>
                }
            }
        </div>
    
        if (totalPages > 1)
        {
            <nav aria-label="Page navigation example">
                <ul class="pagination">
                    @if (page > 1)
                        {
                        <li class="page-item">
                            <a href="@pageURL@(page - 1)" class="page-link" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                                <span class="sr-only">Previous</span>
                            </a>
                        </li>
                    }
    
                    @for (int p = 1; p < totalPages + 1; p++)
                        {
                            var active = (p == page) ? "active" : string.Empty;
    
                        <li class="page-item @(Html.Raw(active))">
                            <a href="@pageURL@p" class="page-link">@p</a>
                        </li>
                    }
    
                    @if (page < totalPages)
                        {
                        <li class="page-item">
                            <a href="@pageURL@(page + 1)" class="page-link" aria-label="Previous">
                                <span aria-hidden="true">&raquo;</span>
                                <span class="sr-only">Previous</span>
                            </a>
                        </li>
                    }
                </ul>
            </nav>
        }
    
    }
    else
    {
        <p>No posts found</p>
    }
    

    Thank you

Please Sign in or register to post replies

Write your reply to:

Draft