CMS Development

HBarden
Participant

Fixing Internal Broken Page Number Links

SOLVE

Hi, 

 

I'm working on the SEO of a website and struggling to fix these broken links. The links are due to the page numbering on a blog. So the number/arrow selector that advances the pages starts with page 0 and if you click to go back, you get to -1 which isn't an existing page. 

 

I believe the issue lies somewhere within the coding for the advancement of pages as the broken link occurs across different pages that feature that feature.

 

I'll paste the content code below. If someone could help me correct this, that would be great. Thanks.

 

<div class="blog-pagination">
{% if contents.total_page_count > 0 %}
{% if widget.range == 10 %}
{% set base = 10 %}
{% set loop_ = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] %}
{% else %}
{% set base = 5 %}
{% set loop_ = ['1', '2', '3', '4', '5'] %}
{% endif %}

{% set current_pg_index = last_page_num + 1 %}
{% set range_start = ((1 - current_pg_index) % base) + current_pg_index %}

{# EXTRA: calculate start of previous range and end of next
{% set range_end = range_start + (base)-1 %}

{% set previous_range_start = ((6 - current_pg_index) % base) + current_pg_index - base %}
{% if previous_range_start < 1 %}
{% set previous_range_start = 1 %}
{% endif %}
#}
<ul>
<li class="first-posts-link {% if current_pg_index <= 1 %}hide{% endif %}"><a href="{{ blog_page_link(1) }}"><i class="fa fa-angle-double-left"></i></a></li>
<li class="previous-posts-link {% 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="pg">
{% set i = range_start %}
{% for pg in loop_ %} {# effectively, for i=1 to i=base #}
{% if i <= contents.total_page_count %}
<li{% if i == current_pg_index %} class="active"{% endif %}><a href="{{ blog_page_link(i) }}">{{ i }}</a></li>

{% set i = i + 1 %}
{% endif %}
{% endfor %}
</span>
<li class="next-posts-link {% if current_pg_index == 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 {% if current_pg_index == 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>
{% endif %}
</div>

0 Upvotes
1 Accepted solution
BarryGrennan
Solution
Top Contributor

Fixing Internal Broken Page Number Links

SOLVE

So I think what's throwing you off is that the blog loop doesn't start at 0. mysite.com/blog and mysite.com/blog/page/1 are the same page

 

For your previous page button use something like 

  <li class="{{ 'hide' if !last_page_num }}">  <a class="pagination__link pagination__link--prev" {% if last_page_num %}href="{% if current_page_num == 2 %}{{ group.absolute_url }}{% else %}{{ blog_page_link(last_page_num) }}{% endif %}"{% endif %}>Previous</a></li>

 

I've pulled from the boilerplate so some of the classes are still in there, change as needed.

 


profile2022aBarry Grennan

Freelance HubSpot CMS Developer

Website | Contact | LinkedIn

 

 

View solution in original post

2 Replies 2
BarryGrennan
Solution
Top Contributor

Fixing Internal Broken Page Number Links

SOLVE

So I think what's throwing you off is that the blog loop doesn't start at 0. mysite.com/blog and mysite.com/blog/page/1 are the same page

 

For your previous page button use something like 

  <li class="{{ 'hide' if !last_page_num }}">  <a class="pagination__link pagination__link--prev" {% if last_page_num %}href="{% if current_page_num == 2 %}{{ group.absolute_url }}{% else %}{{ blog_page_link(last_page_num) }}{% endif %}"{% endif %}>Previous</a></li>

 

I've pulled from the boilerplate so some of the classes are still in there, change as needed.

 


profile2022aBarry Grennan

Freelance HubSpot CMS Developer

Website | Contact | LinkedIn

 

 

Jaycee_Lewis
Community Manager
Community Manager

Fixing Internal Broken Page Number Links

SOLVE

Hi, @HBarden 👋 Thanks for reaching out. Hey, @BarryGrennan @Indra @jpsanchez, do you have any thoughts here?

 

Thank you!  — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes