New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to display a fixed number of posts per page (i.e. pagination) #16

Open
wants to merge 2 commits into
base: master
from

Conversation

2 participants
@yihui
Owner

yihui commented Oct 27, 2017

Normally when you list posts, you use something like range .Data.Pages, which will show all posts in a single list. Sometimes when you have a large number of posts, you may want to show, for example, 10 posts per page, and provide a navigation menu so that readers can go to the next/previous 10 posts. In this case, you can apply .Paginate to .Data.Pages, e.g. range (.Paginate .Data.Pages), and Hugo will automatically retrieve a subset of posts for you to list.

Hugo has provided an internal pagination template to build the navigation menu that consists of page numbers:

{{ template "_internal/pagination.html" . }}

Lastly, you may need to tweak the CSS to display the navigation menu appropriately.

In this example, I set paginate = 1 in config.toml, which is an extreme case. Its default value is 10 (i.e. 10 posts per page). I only have four posts on this example website, and I just want to display them on four pages as an example.

Preview:

Documentation of pagination in Hugo: https://gohugo.io/templates/pagination/

@yihui yihui added the feature label Oct 27, 2017

@yihui yihui changed the title from How to display a fixed number of posts per page to How to display a fixed number of posts per page (i.e. pagination) Oct 27, 2017

yihui added some commits Oct 27, 2017

test pagination (show one post per page; it does not make practical s…
…ense, but I only have four pages on this example site in total, and I want to show them in four pages)
@jrosen48

This comment has been minimized.

jrosen48 commented Oct 8, 2018

Hi, wondering what you are thinking about merging this - I'm interested in using this functionality! Tnx

@yihui

This comment has been minimized.

Owner

yihui commented Oct 8, 2018

Here is the background story of all these pull requests: https://yihui.name/en/2017/09/pull-requests-as-a-teaching-tool/

@jrosen48

This comment has been minimized.

jrosen48 commented Oct 9, 2018

Cool. Thanks.

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