jekyll - Hierarchical Categories in GitHub Pages -


i'm using jekyll on github pages, , want have hierarchical categories this:

  • animals -> mammals -> cats -> _posts -> housecat.md, tiger.md
  • animals -> mammals -> dogs -> _posts -> poodle.md, doberman.md
  • animals -> reptiles -> lizards -> _posts -> iguana.md, chameleon.md

i'd users able visit /animals , see listing of every post every category. if go /animals/mammals, they'd see mammals. if go /animals/mammals/cats, see cats.

i know can manually putting index.html file in every single directory , looping through site.categories.mammals or site.categories.cats, example.

but seems little bit brute force, , i'm hoping there's better way. if want change how i'm showing listings, i'll have change in every single subcategory. i'll have problems when subcategories share name, /abc/xyz/_posts/one.md , /def/xyz/_posts/two.md.

i've tried follow this article, uses 1 main category.html page loops through page.category:

{% post in site.categories.[page.category] %}   <h2><a href=""></a></h2>   <p></p> {% endfor %} 

then every index.html file uses layout. works, seems limited 1 category, not multiple hierarchical categories.

is there less brute-force approach creating listings hierarchical categories?

page.categories list

https://stackoverflow.com/a/23927986

{% cat in page.categories %}   <h1>{{ cat }}</h1>   <ul>     {% post in site.categories[cat] %}       <li><a href="{{ post.url }}">{{ post.title }}</a></li>     {% endfor %}   </ul> {% endfor %} 

from jekyll's documentation page.category http://jekyllrb.com/docs/variables/#page-variables

the list of categories post belongs. categories derived directory structure above _posts directory. example, a post @ /work/code/_posts/2008-12-24-closures.md have this field set ['work', 'code']. these can specified in yaml front matter.

you should able add simple dynamic index.html every folder , categories should hierarchical automatically.

update

the above not work. need treat combination of categories of each hierarchy unique item. concat index page's categories, , compare against posts in site.

/foo/bar/_posts/2016-08-01-foo-bar-test.html

--- categories:   - foo   - bar title: test foo bar ---  <h2>foo bar</h2> 

/var/bar/_posts/2016-08-01-test-var-bar.html

--- categories:   - var   - bar title: test var bar ---  <h2>var bar</h2> 

/foo/bar/index.html

--- categories:  - foo  - bar ---  {% assign pagecat = page.categories | join ' ' | append: ' '%} {% assign pagecatlen = page.categories.size %}   <h1>{{ cat }}</h1>   <ul>     {% post in site.posts %}     {% assign postcat = '' %}     {% thispostcat in post.categories limit: pagecatlen %}       {% assign postcat = postcat | append: thispostcat %}       {% assign postcat = postcat | append: ' ' %}     {% endfor %}      {% if (postcat == pagecat) %}       <li><a href="{{ post.url }}">{{ post.title }}</a></li>     {% endif %}     {% endfor %}   </ul> 

the categories optional files in _posts, required front matter of each index file.


Comments