A page to show posts belonging to 2 categories

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?

Try tweaking this:

<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.

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

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.

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…