Skip to content

Instantly share code, notes, and snippets.

@eashish93
Last active February 9, 2023 06:15
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Ghost numbered pagination

Hye, this is custom numbered pagination with first, next and prev links. Also the user can limit the pagination numbers. Just include this code in pagination.hbs file inside ghost theme partials directory. Any improvements are always welcome.

{{!-- custom pagination file --}}

<nav class="pagination" role="navigation">
    {{#if prev}}
    <a class="prev" href="{{page_url prev}}">&larr; Prev</a>
     {{/if}}
     <div class="page-nums">
         {{!-- <a href="" class="current">{{page}}</a>
         <a href="">1</a> --}}
     </div>
     {{#if next}}
     <a class="next" href="{{page_url next}}">Next &rarr;</a>
      {{/if}}
</nav>

<script>
    var pageTotal = parseInt("{{pages}}"),
        pageCurrent = parseInt("{{page}}"),
        limitPage = 5,
        page = 1,
        pageNums = document.querySelector('.pagination .page-nums');


    if(pageCurrent >= limitPage) {
        page = pageCurrent - 1;
        pageNums.innerHTML += '<a class="first" href="{{page_url}}page/' + 1 + '">First</a>';
    }

    for(i = 0; i < limitPage; i++) {

        if(page === pageCurrent) {
            pageNums.innerHTML += '<a class="current" href="{{page_url}}page/{{page}}">{{page}}</a>';
        }
        else if(page <= pageTotal){
            pageNums.innerHTML += '<a href="{{page_url}}page/' + page + '">'  + page + '</a>'
        }
        page++;
    }

</script>
@kuzyk
Copy link

kuzyk commented Aug 16, 2022

as for me this solution is simple

<div class="blog-pagination">
    <a href="{{page_url prev}}" class="back"></a>

    {{#foreach '____________________________________________________________' from=1 to=pages}}
        <a href="{{page_url @number}}" class="{{#match @number '=' ../page}}current{{/match}}">
            {{@number}}
        </a>
    {{/foreach}}

    <a href="{{page_url next}}" class="next"></a>
</div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment