Skip to content

Instantly share code, notes, and snippets.

@eashish93
Last active October 6, 2023 11:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eashish93/c7b479ceebc72f9e988b8d37bc84af75 to your computer and use it in GitHub Desktop.
Save eashish93/c7b479ceebc72f9e988b8d37bc84af75 to your computer and use it in GitHub Desktop.
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>

@VeloAddict
Copy link

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>

This works better.
What could be done to replace the data part with something else than _______?

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