5

I am trying to create a navigation bar displaying all the categories and then by clicking on each category, it then links to all the posts in that category.

I tried below, it displays all the categories but not as a link.

{% for category in site.categories %}
   <div class= "categories-title"><a name="{{ category | first }}">{{ category | first   }}</a></div>   
{% endfor %}

I also tried jekyll-category-archive-plugin as below, but it gives Error: Unknown tag 'category'.

{% for category in site categories %}
  {% category link category %}This is a link to {{category}} {% endcategorylink %}
{% endfor %}

Can anyone give me some tips how to best do this?

Thanks a lot. Jing

Jing
  • 51
  • 1
  • 3

2 Answers2

6

There's another solution that works on GitHub Pages:
One single page that contains all posts for all categories.

I answered a similar question here where I showed how to do this:
An easy way to support tags in a jekyll blog

In my answer, I'm using tags instead of categories, but as far as I know, both work exactly the same way.
(so you can just take my code and replace site.tags by site.categories)

The generated HTML for each tag will look something like this:

  <h3 id="jekyll">jekyll</h3>
  <ul>
    <li>
      <a href="/blah/">Newest Jekyll post</a>
    </li>
    <li>
      <a href="/foo/">Older Jekyll post</a>
    </li>
  </ul>

That was the page which displays all posts for each category.
Now to the categories list in the navigation bar.

Again, take a look at the HTML above:
Thanks to the id="jekyll" part, you can use the link /tags/#jekyll to load the /tags/ page and directly jump to the Jekyll tag.

On my site, I'm using this everywhere where I'm linking to the /tags/ page.

To create these links in your navigation bar as well, you just need to take the first example code from your question and change this:

<a name="{{ category | first }}">

...to this:

<a href="/tags/#{{ category | first }}">

(I'll just assume that your categories page is under the URL /tags/ as well, like in my example)

So the complete code will look like that:

{% for category in site.categories %}
    <div class="categories-title"><a href="/tags/#{{ category | first }}">{{ category | first }}</a></div>   
{% endfor %}

The generated HTML will have a link like the following, for each category:

<div class="categories-title"><a href="/tags/#jekyll">jekyll</a></div> 

EDIT:

You wrote in a comment:

I see that you have all tags with posts on one page. I have created a categories page and I would like to use this page as a template. While clicking each category in the navigation bar, I would like it to link to its own page.

In the meantime, I wrote a blog post about building separate category pages without a plugin:
Separate pages per tag/category with Jekyll (without plugins)

Community
  • 1
  • 1
Christian Specht
  • 35,843
  • 15
  • 128
  • 182
  • hi Christian, thanks a lot for your help. I see that you have all tags with posts on one page. I have created a categories page and I would like to use this page as a template. While clicking each category in the navigation bar, I would like it to link to its own page. I will try to modify your codes and get it work hopefully. thanks again :) – Jing Oct 06 '14 at 21:05
  • You can either use a plugin to generate multiple category pages ([this one](https://github.com/recurser/jekyll-plugins/blob/master/generate_categories.rb), for example), but that won't work on GitHub Pages. If you can't use a plugin for any reasons, you have two choices left: a) my solution above *or* b) create all category pages as separate pages by hand – Christian Specht Oct 06 '14 at 22:11
  • Hi Christian, I used the plugin you referred to and it worked. Thanks a lot. Now I get some complication. I would like to build a multilingual sites (en and cn). I moved posts into separate folders such as CN/_posts and EN/_posts for example. Then it messed up. Now I am trying to play with the jekyll-multi-languages plug in, hopefully it may work. if you have any more tips regarding this, please let me know. – Jing Oct 23 '14 at 15:28
1

Jekyll doesn't render archive pages automatically like category pages by default. You have to create category pages yourself or use a plugin like »Category archive plugin for Jekyll«. But I guess this won't work, if you use GitHub Pages with Jekyll.

Phlow
  • 688
  • 4
  • 14