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
@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.

@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
Copy link
Owner Author

yihui commented Mar 17, 2020

@jrosen48 Yes, I have updated this PR.

…ense, but I only have four pages on this example site in total, and I want to show them in four pages)
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