Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- link: https://community.hubspot.com/t5/Content-Design-Questions/Pagination-not-working-with-offset-posts-Hubl/m-p/152866#M4331
- I've got this solved. I've posted my snippets below in case anyone else runs into this same thing.
- tl;dr pagination won't work with anything other than the default {% content in contents %} loop.
- My issue in a nutshell was that I needed multiple loops with offsets and post limits because their layouts were different enough that I couldn't just use CSS with nth-child.
- For blog hero I pulled in the first post and limited it to one, total post in the loop using loop.index.
- {% for content in contents %}
- {% if loop.index <= 1 %}
- {% set post_feat = content.featured_image %}
- {% set post_url = content.absolute_url %}
- {% set post_title = content.title %}
- <section class="hero-banner" style="background-image: url('{{ post_feat }}');">
- <div class="blog-hero-wrapper">
- <div class="blog-hero-content">
- <div class="blog-hero-cats">
- {% if content.topic_list %}
- {% for topic in content.topic_list %}
- {% if loop.index <= 3 %}
- <a class="topic-link" href="{{ group.absolute_url }}/topic/{{ topic.slug }}">{{ topic.name }}</a>
- {% endif %}
- {% endfor %}
- {% endif %}
- </div>
- <h2 class="blog-hero-title"><a href="{{ post_url }}">{{ post_title }}</a></h2>
- {% if content.blog_post_author %}
- <div class="blog-hero-author-wrapper">
- <a class="blog-author-link" href="{{ group.absolute_url }}/author/{{ content.blog_post_author.slug }}"><img src="{{ content.blog_post_author.avatar }}" alt="{{ content.blog_post_author.display_name }}">
- <span>by {{ content.blog_post_author.display_name }}</span>
- </a>
- </div>
- {% endif %}
- <div class="blog-hero-excerpt">
- {{ content.post_list_content|striptags|truncate(150, False, '...') }}
- </div>
- <a class="blog-hero-btn" href="{{ post_url }}" title="post_title">Keep Reading <i class="im-icon-arrow-right-5"></i></a>
- </div>
- </div>
- </section>
- {# end loop for first post #}
- {% endif %}
- {% endfor %}
- For the next 6 posts, the first 3 were full width, half and half layouts. The second three were 1/3 width with a different layout.
- I have two loops. For the first, I skipped the first post using loop.index > 1 and loop.index <= to limit it to posts 2-4.
- Similiarly for the second loop, I skipped the first four posts, and limited the output to three (posts 5-7).
- I'm happy to say this worked well with pagination as well as Author and Topic pages without extra conditionals for those pages.
- <div class="blog-section listing-page">
- <div class="blog-listing-wrapper">
- <div class="post-listing blog-top-three">
- {# start default loop #}
- {% for content in contents %}
- {% if loop.index > 1 %}
- {% if loop.index <= 4 %}{# for the first three posts #}
- <div class="post-item full-width">
- <div class="container">
- <div class="row-fluid">
- <div class="span6 post-content">
- {% if content.topic_list %}
- <div class="blog-cats">
- {% for topic in content.topic_list %}
- {% if loop.index <= 3 %}
- <a class="topic-link" href="{{ group.absolute_url }}/topic/{{ topic.slug }}">{{ topic.name }}</a>
- {% endif %}
- {% endfor %}
- </div>
- {% endif %}
- <h2 class="blog-header"><a href="{{ content.absolute_url }}">{{ content.title }}</a></h2>
- {% if content.blog_post_author %}
- <p class="byline">By <a href="{{ group.absolute_url }}/author/{{ content.blog_post_author.slug }}" class="author-name">{{ content.blog_post_author.display_name }}</a></p>
- {% endif %}
- <div class="post-excerpt">
- {{ content.post_list_content|striptags }}
- </div>
- <a class="blog-read-more-btn" href="{{ content.absolute_url }}" title="{{ content.title }}">Keep Reading</a>
- </div><!-- / .post-content -->
- <div class="span6 post-feat-img">
- <a href="{{ content.absolute_url }}" title="{{ content.title }}" style="background-image: url({{ content.featured_image }});"></a>
- </div>
- </div><!-- /.row-fluid -->
- </div><!-- /.container -->
- </div><!-- ./post-item -->
- {% endif %}{# end for the first three posts #}
- {% endif %}{# end skip first post #}
- {% endfor %}{# end contents loop #}
- </div><!-- post-listing blog-top-three -->
- <div class="post-listing blog-bottom-three">
- <div class="container">
- <div class="row-fluid">
- {% for content in contents %}{# restart loop #}
- {% if loop.index > 4 %}
- {% if loop.index <= 7 %}{# for second three posts #}
- <div class="post-item span4">
- <div class="post-content">
- <div class="post-feat-img">
- <a href="{{ content.absolute_url }}" title="{{ content.title }}" style="background-image: url({{ content.featured_image }});"></a>
- </div>
- {% if content.topic_list %}
- <div class="blog-cats">
- {% for topic in content.topic_list %}
- {% if loop.index <= 3 %}
- <a class="topic-link" href="{{ group.absolute_url }}/topic/{{ topic.slug }}">{{ topic.name }}</a>
- {% endif %}
- {% endfor %}
- </div>
- {% endif %}
- <h2 class="blog-header"><a href="{{ content.absolute_url }}">{{ content.title }}</a></h2>
- <div class="post-excerpt">
- {{ content.post_list_content|striptags }}
- </div>
- <a class="blog-read-more-btn" href="{{ content.absolute_url }}" title="{{ content.title }}">Keep Reading</a>
- </div><!-- / .post-content -->
- </div><!-- /.post-item -->
- {% endif %}{# end for the second three posts #}
- {% endif %}{# end skip four posts #}
- {% endfor %}{# end default loop #}
- </div><!-- row fluid -->
- </div><!-- container -->
- </div><!-- /.blog-bottom-three -->
- {# begin pagination #}
- {% set total_pages = contents.total_page_count %} <!-- sets variable for total pages -->
- {% set more_pages = total_pages - current_page_num %} <!-- sets variable for how many more pages are past the current pages -->
- {% if total_pages > 1 %}
- <nav class="pagination-wrapper">
- <ul class="pagination">
- <li class="{% if current_page_num == 1 %}disabled{% endif %}"><a href="{% if last_page_num=="1" %}{{ group.absolute_url }}{% else %}{{blog_page_link(last_page_num)}}{% endif %}"><i class="fa fa-angle-double-left"></i></a></li>
- {% if more_pages == 0 %}
- {% if current_page_num - 4 == 1 %}<li><a href="{{ group.absolute_url }}">{{ current_page_num - 4 }}</a></li>{% endif %}
- {% if current_page_num - 4 > 1 %}<li><a href="{{ blog_page_link(current_page_num - 4) }}">{{ current_page_num - 4 }}</a></li>{% endif %}
- {% endif %}
- {% if more_pages <= 1 %}
- {% if current_page_num - 3 == 1 %}<li><a href="{{ group.absolute_url }}">{{ current_page_num - 3 }}</a></li>{% endif %}
- {% if current_page_num - 3 > 1 %}<li><a href="{{ blog_page_link(current_page_num - 3) }}">{{ current_page_num - 3 }}</a></li>{% endif %}
- {% endif %}
- {% if current_page_num - 2 == 1 %}<li><a href="{{ group.absolute_url }}">{{ current_page_num - 2 }}</a></li>{% endif %}
- {% if current_page_num - 2 > 1 %}<li><a href="{{ blog_page_link(current_page_num - 2) }}">{{ current_page_num - 2 }}</a></li>{% endif %}
- {% if current_page_num - 1 == 1 %}<li><a href="{{ group.absolute_url }}">{{ current_page_num - 1 }}</a></li>{% endif %}
- {% if current_page_num - 1 > 1 %}<li><a href="{{ blog_page_link(current_page_num - 1) }}">{{ current_page_num - 1 }}</a></li>{% endif %}
- <li class="active"><a href="{% if current_page_num==1 %}{{ group.absolute_url }}{% else %}{{ blog_page_link(current_page_num) }}{% endif %}">{{ current_page_num }}</a></li>
- {% if current_page_num + 1 <= total_pages %}<li><a href="{{ blog_page_link(current_page_num + 1) }}">{{ current_page_num + 1 }}</a></li>{% endif %}
- {% if current_page_num + 2 <= total_pages %}<li><a href="{{ blog_page_link(current_page_num + 2) }}">{{ current_page_num + 2 }}</a></li>{% endif %}
- {% if current_page_num <= 2 %}
- {% if current_page_num + 3 <= total_pages %}<li><a href="{{ blog_page_link(current_page_num + 3) }}">{{ current_page_num + 3 }}</a></li>{% endif %}
- {% endif %}
- {% if current_page_num == 1 %}
- {% if current_page_num + 4 <= total_pages %}<li><a href="{{ blog_page_link(current_page_num + 4) }}">{{ current_page_num + 4 }}</a></li>{% endif %}
- {% endif %}
- <li class="{% if current_page_num == total_pages %}disabled{% endif %}"><a href="{{blog_page_link(next_page_num)}}"><i class="fa fa-angle-double-right"></i></a></li>
- </ul>
- </nav>
- {% endif %}
- </div><!-- /.blog-listing-wrapper -->
- </div><!-- /.blog-section -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement