# How to Link to Next and Previous Posts for Same Blog Category

**URL:** <https://talk.jekyllrb.com/t/how-to-link-to-next-and-previous-posts-for-same-blog-category/629>\
**Category:** Help\
**Created:** [June 8, 2017, 7:57pm UTC](https://talk.jekyllrb.com/t/how-to-link-to-next-and-previous-posts-for-same-blog-category/629 "2017-06-08T19:57:50Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![tgherzog](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/tgherzog/32/2702_2.png) [@tgherzog](https://talk.jekyllrb.com/u/tgherzog)\
**Post date:** [July 25, 2020, 10:33pm UTC](https://talk.jekyllrb.com/t/how-to-link-to-next-and-previous-posts-for-same-blog-category/629/5 "2020-07-25T22:33:06Z")

</div>

Here’s an alternate version of the Liquid code snippet above. It only iterates over the categories list once, and it exits as soon as it finds a match, so it should be substantially faster, in case you are not able to use the plugin. Note that in case of multiple categories for the post, this uses the first one.

```
{% assign cat = page.categories[0] %}
{% assign cat_list = site.categories[cat] %}
{% for post in cat_list %}
  {% if post.url == page.url %}
  	{% assign pOffset = forloop.index0 | minus: 1 %}
  	{% assign nOffset = pOffset | plus: 2 %}
  	{% if forloop.first == false %}
  	  {% assign next_post = cat_list[pOffset] %}
  	{% endif %}
  	{% if forloop.last == false %}
  	  {% assign previous_post = cat_list[nOffset] %}
  	{% endif %}
  	{% break %}
  {% endif %}
{% endfor %}
```

---

_[View the full topic](https://talk.jekyllrb.com/t/how-to-link-to-next-and-previous-posts-for-same-blog-category/629)._
