Liquid paginate: Split into multiple pages
- Liquid paginate: Split into multiple pages
-
When you have a long list of items, such as products, blog posts, or collections, displaying them all on a single page can make it overwhelming for users and slow down your Shopify store’s loading time. This is where Shopify’s paginate tag comes to the rescue. It allows you to split your content into multiple pages, improving user experience and site performance.
In this comprehensive guide, we’ll delve into the intricacies of the paginate tag, exploring its syntax, parameters, and practical applications.
The paginate tag works with the for tag to divide your content into different pages. It must wrap a for tag block that can loop through an array as displayed in the example taken below:{% paginate collection.products by 5 %} {% for product in collection.products %} <!--show product details here --> {% endfor %} {% endpaginate %}
The parameter called
by
is followed by an integer between 1 and 50 that could tell thepaginate
tag how many results it should output per page. Withinpaginate
tags, you are allowed to access attributes of the paginate object. This includes the attributes to output the links, which are required to navigate within the created pages.Now, let’s take a look at our instructional writing on Liquid paginate: Split into multiple pages to know more deeply about the use of ‘include’.
Liquid paginate: Split into multiple pages
- The use of Pagination
- How to enable Pagination
- About Liquid Attributes Available
- How to render the paginated Posts
The use of Pagination
It is very common, with many websites especially blogs, to break the main listing of posts up into smaller lists and show them over many pages. Then Jekyll offers a pagination plugin so you are able to automatically generate the appropriate files and folders you need for paginated listings.
How to enable Pagination
Please insert a line into the
_config.yml
file, which can specify how many items should be shown per page, to enable pagination for posts on your blog:paginate: 5
The number should be the maximum number of posts you would like to display per page in the created site.
You might also want to specify the destination of the pagination pages:
paginate_path: "/blog/page:num/"
This will read in
blog/index.html
, send it every pagination page in Liquid aspaginator
and type in the output toblog/page:num/
, the place that:num
is the pagination page number, which starts with2
. In case a site has 12 posts and specifiespaginate: 5
, Jekyll will writeblog/index.html
with the first 5 posts,blog/page2/index.html
with the following 5 posts andblog/page3/index.html
with the last 2 posts into the destination directory.About Liquid Attributes Available
The pagination plugin exposes the
paginator
liquid object with the attributes listed below:VARIABLE DESCRIPTION paginator.per_page
The number of the current page paginator.page
The number of the current page paginator.next_page_path
The path to the next page or nil
if no subsequent page existspaginator.next_page
The number of the next page or nil
if no subsequent page existspaginator.previous_page_path
The path to the previous page or nil
if no previous page existspaginator.previous_page
The number of the previous page or nil
if no previous page existspaginator.total_pages
Total number of pages paginator.total_posts
Total number of posts paginator.posts
Posts available for the current page How to render the paginated Posts
The next step you need to do is to actually show your posts in a list using the
paginator
variable that will be available to you now. You will propably want to do this in one of the main pages of your site. Here do we give you an example displayed below as a simple way of rendering paginated Posts in a HTML file:--- layout: default title: My Blog --- <!-- This loops through the paginated posts --> {% for post in paginator.posts %} <h1><a href="/%7B%7B%20post.url%20%7D%7D">{{ post.title }}</a></h1> <p class="author"> <span class="date">{{ post.date }}</span> </p> <div class="content"> {{ post.content }} </div> {% endfor %} <!-- Pagination links --> <div class="pagination"> {% if paginator.previous_page %} <a href="/%7B%7B%20paginator.previous_page_path%20%7D%7D" class="previous"> Previous </a> {% else %} <span class="previous">Previous</span> {% endif %} <span class="page_number "> Page: {{ paginator.page }} of {{ paginator.total_pages }} </span> {% if paginator.next_page %} <a href="/%7B%7B%20paginator.next_page_path%20%7D%7D" class="next">Next</a> {% else %} <span class="next ">Next</span> {% endif %} </div>
The HTML snippet below should handle page one and render a list of every page with links to all except for the recent page.
{% if paginator.total_pages > 1 %} <div class="pagination"> {% if paginator.previous_page %} <a href="/%7B%7B%20paginator.previous_page_path%20%7C%20relative_url%20%7D%7D"> « Prev </a> {% else %} <span>« Prev</span> {% endif %} {% for page in (1..paginator.total_pages) %} {% if page == paginator.page %} <em>{{ page }}</em> {% elsif page == 1 %} <a href="/%7B%7B%20paginator.previous_page_path%20%7C%20relative_url%20%7D%7D"> {{ page }} </a> {% else %} <a href="{{ site.paginate_path | relative_url | replace: ':num', page }}"> {{ page }} </a> {% endif %} {% endfor %} {% if paginator.next_page %} <a href="/%7B%7B%20paginator.next_page_path%20%7C%20relative_url%20%7D%7D"> Next » </a> {% else %} <span>Next »</span> {% endif %} </div> {% endif %}
Conclusion
Mastering the paginate tag in Shopify’s Liquid templating language is a valuable skill for any theme developer or store owner looking to optimize their online store’s user experience and performance. Remember, the paginate tag is just one tool in your Shopify arsenal. Combine it with other Liquid objects and filters to create a truly customized and user-friendly storefront.
Sam Nguyen is the CEO and founder of Avada Commerce, an e-commerce solution provider headquartered in Singapore. He is an expert on the Shopify e-commerce platform for online stores and retail point-of-sale systems. Sam loves talking about e-commerce and he aims to help over a million online businesses grow and thrive.Related Post
-
Top 7+ Free Shopify Apps for Your Shopify Store
To run a successful Shopify store, you should find the right tools to streamline your operations ...December 30, 2024 -
TOP 5+ Shopify Retargeting Apps 2025: Boost Sales and Optimize Conversions
Are you looking to boost your Shopify store’s conversions and re-engage lost visitors? In this bl...December 13, 2024 -
Top 9 Color Swatches Apps for Your Shopify Store in 2025
When people shop online, they want a website that is easy to use and looks good. If your products...December 12, 2024 -
Top 10 Shopify Apps 2025 to Create Urgency and Boost Sales
In this blog post, we’ll dive into the top Shopify urgency apps that can boost your store’s sales...December 12, 2024 -
Top 10 Shopify Shoes Themes for 2025: Enhance Your Store’s Design and Functionality
In 2024, creating an engaging and functional online shoe store has never been easier with Shopify...December 12, 2024 -
Top 10 Shopify Announcement Bar Apps In 2025
Many Shopify store owners struggle to grab customers’ attention quickly and share important...December 10, 2024
-