# A page to show posts belonging to 2 categories

**URL:** <https://talk.jekyllrb.com/t/a-page-to-show-posts-belonging-to-2-categories/4823>\
**Category:** Help\
**Created:** [August 8, 2020, 12:21pm UTC](https://talk.jekyllrb.com/t/a-page-to-show-posts-belonging-to-2-categories/4823 "2020-08-08T12:21:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rampatra](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/rampatra/32/1929_2.png) [@rampatra](https://talk.jekyllrb.com/u/rampatra)\
**Post date:** [August 8, 2020, 12:21pm UTC](https://talk.jekyllrb.com/t/a-page-to-show-posts-belonging-to-2-categories/4823/1 "2020-08-08T12:21:58Z")

</div>

I want to create a generic page to show posts belonging to two categories.

For example,

- If the URL is `www.blog.com/category/question/ruby` then show posts belonging to category `question` and `ruby`.
- If the URL is `www.blog.com/category/question/java` then show posts belonging to category `question` and `java`.
- If the URL is `www.blog.com/category/scala` then show posts belonging to category `scala` only.

Is this possible with `jekyll-archive` plugin or is there a better way?

---

<div class="post-metadata">

**Author:** ![xplosionmind](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/xplosionmind/32/4041_2.png) [@xplosionmind](https://talk.jekyllrb.com/u/xplosionmind)\
**Post date:** [August 14, 2020, 6:48am UTC](https://talk.jekyllrb.com/t/a-page-to-show-posts-belonging-to-2-categories/4823/2 "2020-08-14T06:48:52Z")

</div>

Try tweaking this:

```auto
<h2>Question + Ruby</h1>
<ul>
{% for post in site.posts %}
  {% if post.categories contains 'question' %}
    {% if post.categories contains 'ruby' %}
      <li><a href="{{ post.url }}">{{post.title}}</a></li>
    {% endif %}
  {% endif %}
{% endfor %}
</ul>
<br />
<h2>Question + Java</h1>
<ul>
{% for post in site.posts %}
  {% if post.categories contains 'question' %}
    {% if post.categories contains 'Java' %}
      <li><a href="{{ post.url }}">{{post.title}}</a></li>
    {% endif %}
  {% endif %}
{% endfor %}
</ul>

```

This is pretty basic and could definitely be optimized, but it’s a start.

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [August 18, 2020, 7:25am UTC](https://talk.jekyllrb.com/t/a-page-to-show-posts-belonging-to-2-categories/4823/3 "2020-08-18T07:25:57Z")

</div>

Solution above is good.

For a general approach you could go through posts and filter to were posted.categories.size \> 1

Then show the categories next to the post.

If you wanted to group them under headings, I don’t know what is best to do

---

<div class="post-metadata">

**Author:** ![rampatra](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/rampatra/32/1929_2.png) [@rampatra](https://talk.jekyllrb.com/u/rampatra)\
**Post date:** [August 18, 2020, 10:02am UTC](https://talk.jekyllrb.com/t/a-page-to-show-posts-belonging-to-2-categories/4823/4 "2020-08-18T10:02:22Z")

</div>

I get this solution but it isn’t what I asked for, unfortunately.

With `jekyll-archive` plugin, you get pages automatically (with URL like `www.blog.com/category/question/` and `www.blog.com/category/ruby/`) that show posts of category `question` and `ruby` respectively.

So, is there a way to get pages automatically that will show posts belonging to 2 categories?

The solution mentioned will require me to write code for each and every category in my blog. I want this to be automated like in `jekyll-archive` plugin. Hope I am making sense.

---

<div class="post-metadata">

**Author:** ![mmistakes](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/mmistakes/32/2169_2.png) [@mmistakes](https://talk.jekyllrb.com/u/mmistakes)\
**Post date:** [August 18, 2020, 12:59pm UTC](https://talk.jekyllrb.com/t/a-page-to-show-posts-belonging-to-2-categories/4823/5 "2020-08-18T12:59:19Z")

</div>

Have a look at the jekyll-paginate-v2 plugin. It’s name is a bit misleading, but in addition to pagination it can create archive pages for categories, tags, collections, etc. It provides a lot more control over how those pages are created and I believe you can achieve what you want with it…

> <https://github.com/sverrirs/jekyll-paginate-v2/blob/master/README-GENERATOR.md#paginate-categories-tags-locales>
