Skip to content
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
Choose a base branch
from

Conversation

yihui
Copy link
Owner

@yihui 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 How to display a fixed number of posts per page How to display a fixed number of posts per page (i.e. pagination) Oct 27, 2017
@yihui yihui force-pushed the feature/pagination branch from 43bbb96 to a366614 Compare March 21, 2018 15:39
@yihui yihui force-pushed the feature/pagination branch from a366614 to a37b74e Compare August 30, 2018 15:02
@yihui yihui force-pushed the feature/pagination branch from a37b74e to b59d1fe Compare October 1, 2018 14:33
@jrosen48
Copy link

jrosen48 commented Oct 8, 2018

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

@yihui
Copy link
Owner Author

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
Copy link

jrosen48 commented Oct 9, 2018

Cool. Thanks.

@yihui yihui force-pushed the feature/pagination branch from b59d1fe to a8f4b53 Compare March 20, 2019 02:59
@yihui yihui force-pushed the feature/pagination branch from a8f4b53 to 91dbac3 Compare April 18, 2019 19:01
@jrosen48
Copy link

Given that layouts/_default/list.html now consists of the following code (due to an update to xmin):

<ul>
  {{ $pages := .Pages }}
  {{ if .IsHome }}{{ $pages = .Site.RegularPages }}{{ end }}
  {{ range (where $pages "Section" "!=" "") }}
  <li>

Instead of this:

<ul>
  {{ range (where .Data.Pages "Section" "!=" "") }}
  <li>

Can you advise what changes should be made to layouts/_default/list.html to display a fixed number of posts?

@yihui yihui force-pushed the feature/pagination branch from 91dbac3 to 8b68b15 Compare March 17, 2020 03:07
@yihui
Copy link
Owner Author

yihui commented Mar 17, 2020

@jrosen48 Yes, I have updated this PR.

@yihui yihui force-pushed the feature/pagination branch from 8b68b15 to b8200b5 Compare July 24, 2020 19:48
@yihui yihui force-pushed the feature/pagination branch 2 times, most recently from eeac039 to 485f4f4 Compare October 11, 2020 20:57
@yihui yihui force-pushed the feature/pagination branch from 485f4f4 to 7cffbe3 Compare June 15, 2022 03:53
@yihui yihui force-pushed the feature/pagination branch from 7cffbe3 to 2e6228d Compare February 5, 2024 14:58
yihui added 2 commits March 3, 2024 20:23
…ense, but I only have four pages on this example site in total, and I want to show them in four pages)
@yihui yihui force-pushed the feature/pagination branch from 2e6228d to 87f7e2d Compare March 4, 2024 02:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants