CMS Development

JSC_LBM
Member

Blog listing template allowing 0, -1, -2 page results

Hello,

 

We have a client blog that is allowing for blog.clienturl.com/page/0 to be crawled, causing all kinds of errors when we run SEO. 

 

I have found the code managing the pagination for the listing template, it is as follows:

 

<div class="c-pagination c-resources__pagination-wrapper">
    {% set total_pages = contents.total_page_count %}
    {% set more_pages = total_pages - current_page_num %}
    {% if total_pages > 1 %}
        <nav>
            <ul class="pagination">
              <li class="first-posts-link c-pagination__control c-resources-pagination none w-inline-block {% if current_page_num <= 2 %}hide{% endif %}">
                <a href="{{ blog_page_link(1) }}"><i class="fa fa-angle-double-left"></i></a>
              </li>
              <li class="previous-posts-link c-pagination__control c-resources-pagination none w-inline-block {% if !last_page_num %}hide{% endif %}">
                <a href="{{ blog_page_link(last_page_num) }}"><i class="fa fa-angle-left"></i></a>
              </li>
              <span class="centered-pagination">
                {% if more_pages == 0 %}
                {% if current_page_num - 4 == 1 %}
                <li class="hide-mob c-resources-pagination w-inline-block">
                  <a class="c-paragraph u-np" href="{{ group.absolute_url }}">
                    {{ current_page_num - 4 }}
                  </a>
                </li>
                {% endif %}
                {% if current_page_num - 4 > 1 %}<li class="hide-mob c-resources-pagination w-inline-block"><a class="c-paragraph u-np" 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 class="hide-mob c-pagination__control c-resources-pagination w-inline-block"><a class="c-paragraph u-np" href="{{ group.absolute_url }}">{{ current_page_num - 3 }}</a></li>{% endif %}
                {% if current_page_num - 3 > 1 %}<li  class="hide-mob c-pagination__control c-resources-pagination w-inline-block"><a class="c-paragraph u-np" href="{{ blog_page_link(current_page_num - 3) }}">{{ current_page_num - 3 }}</a></li>{% endif %}
                {% endif %}

                {% if current_page_num - 2 == 1 %}<li class="c-pagination__control c-resources-pagination w-inline-block"><a class="c-paragraph u-np" href="{{ group.absolute_url }}">{{ current_page_num - 2 }}</a></li>{% endif %}
                {% if current_page_num - 2 > 1 %}<li class="hide-mob c-resources-pagination w-inline-block"><a class="c-paragraph u-np" href="{{ blog_page_link(current_page_num - 2) }}">{{ current_page_num - 2 }}</a></li>{% endif %}
                {% if current_page_num - 1 == 1 %}<li class="c-pagination__control c-resources-pagination w-inline-block"><a class="c-paragraph u-np" href="{{ group.absolute_url }}">{{ current_page_num - 1 }}</a></li>{% endif %}
                {% if current_page_num - 1 > 1 %}<li class="c-pagination__control c-resources-pagination w-inline-block"><a class="c-paragraph u-np" href="{{ blog_page_link(current_page_num - 1) }}">{{ current_page_num - 1 }}</a></li>{% endif %}

                <li class="c-pagination__control c-pagination__control--current active c-resources-pagination w-inline-block"><a class="c-paragraph u-np" 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 class="c-pagination__control c-resources-pagination w-inline-block"><a class="c-paragraph u-np" href="{{ blog_page_link(current_page_num + 1) }}">{{ current_page_num + 1 }}</a></li>{% endif %}
                {% if current_page_num + 2 <= total_pages %}<li  class="hide-mob c-pagination__control c-resources-pagination w-inline-block"><a class="c-paragraph u-np" 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 class="hide-mob c-pagination__control c-resources-pagination w-inline-block"><a class="c-paragraph u-np" 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 class="hide-mob c-pagination__control c-resources-pagination w-inline-block"><a class="c-paragraph u-np" href="{{ blog_page_link(current_page_num + 4) }}">{{ current_page_num + 4 }}</a></li>{% endif %}
                {% endif %}
              </span>
              <li class="next-posts-link c-pagination__control c-resources-pagination none w-inline-block {% if current_page_num == contents.total_page_count %}hide{% endif %}">
                <a href="{{ blog_page_link(next_page_num) }}"><i class="fa fa-angle-right"></i></a>
              </li>
              <li class="last-posts-link c-pagination__control c-resources-pagination none w-inline-block {% if current_page_num == contents.total_page_count %}hide{% endif %}">
                <a href="{{ blog_page_link(contents.total_page_count) }}"><i class="fa fa-angle-double-right"></i></a>
              </li>
            </ul>
        </nav>
    {% endif %}
</div>

 

What can be done to force the page to never be less than 1? Thanks!

0 Upvotes
JSC_LBM
Member

Blog listing template allowing 0, -1, -2 page results

I entered this code in place of what I provided above but it completely breaks the pagination styles. Di dyou intend for me to enter that code somewhere in the middle of what I entered? 

 

Also, your working example still allows for a URL to be forced to page 0 then the arrows in the pagination will take you to -1 and a 404.

0 Upvotes
Teun
Key Advisor | Diamond Partner
Key Advisor | Diamond Partner

Blog listing template allowing 0, -1, -2 page results

Hi @JSC_LBM ,

This was just an example of a solution. You could take parts of it to modify your own code.

Could you explain how the URL could be forced to page 0? The 'previous' arrow is not available at the first page of the listing. Please check the example I linked.



Did my answer solve your issue? Help the community by marking it as the solution.
Teun
Key Advisor | Diamond Partner
Key Advisor | Diamond Partner

Blog listing template allowing 0, -1, -2 page results

Hi @JSC_LBM ,

 

Maybe this will help, this should be a bulletproof solution:

 

 

{% if not simple_list_page and query.search == null and contents.total_page_count > 1 %}
{% set page_list = [-2, -1, 0, 1, 2] %}
{% if contents.total_page_count - current_page_num == 1 %}
  {% set offset = -1 %}
{% elif contents.total_page_count - current_page_num == 0 %}
  {% set offset = -2 %}
{% elif current_page_num == 2 %}
  {% set offset = 1 %}
{% elif current_page_num == 1 %}
  {% set offset = 2 %}
{% else %}
  {% set offset = 0 %}
{% endif %}
<div class="c-blog-pagination u-flex u-flex-v-center u-flex-center">
  {% if last_page_num %}
  <a class="c-blog-pagination__button c-blog-pagination__button--prev" href="{{ blog_page_link(last_page_num) }}">
    <span class="c-blog-pagination__button-label">{{module.options.prev}}</span>
  </a>
  {% endif %}


  {% for page in page_list %}
    {% set this_page = current_page_num + page + offset %}
    {% if this_page > 0 and this_page <= contents.total_page_count %}
      <a class="c-blog-pagination__number {% if this_page == current_page_num %}c-blog-pagination__number--active{% endif %}"
        href="{{ blog_page_link(this_page) }}">{{ this_page }}</a>
    {% endif %}
  {% endfor %}

  {% if next_page_num %}
    <a class="c-blog-pagination__button c-blog-pagination__button--next" href="{{ blog_page_link(next_page_num) }}">
      <span class="c-blog-pagination__button-label">{{module.options.next}}</span>
    </a>
  {% endif %}
</div>
{% endif %}

 

 

Working example



Did my answer solve your issue? Help the community by marking it as the solution.
dennisedson
Community Manager
Community Manager

Blog listing template allowing 0, -1, -2 page results

I feel like @Anton could help with this 😉

Thanks,

Dennis




HubSpot Community Developer ShowMake sure to subscribe to our YouTube channel
where you can find the HubSpot Community Developer Show
0 Upvotes