Creating Independant Taxonomy lists on different pages in Grav

Grav is a new content managment system, it is very fast and flexible thanks in part to the highly optimized flat-file architecture and easy on computer resources.

This guide will show you how you can add seperate lists on the sidebar of your blog pages showing different content on each page. Such as latest news in one section and latest blog posts on another section. It takes advantage of the very powerful taxonomy features which are available in Grav.

Remember when customizing anyting within your theme to copy it over and use theme inheritance. You can follow this guide over on the Grav Site to achieve this.

We will use the pages news and blog as an example of how to achieve this. The news section is simply another blog section named differently.

First we will navigate to the appropriate folder within your theme.

$ cd user/themes/deliver/templates/partials

Once in this folder you will see a few files but the important ones for us are sidebar.html.twig and taxonomylist.html.twig. Looking at the taxonomylist.html.twig file you can see what is usually used to create recent blog posts on the sidebar.

$ less taxonomylist.html.twig 
{% set taxlist = taxonomylist.get() %}
{% if taxlist %} 
<ul class="archives">
{% for tax,value in taxlist[taxonomy] %}
<li>
<a href="{{ base_url }}/tag{{ config.system.param_sep }}{{ tax|e('url') }}">{{ tax }} </a>
</li>
{% endfor %}
</ul> {% endif %}

This is the standard taxonomy list. If used on your site all recent posts from every section would be shown on each page. We will create a new file news.taxonomylist.html.twig. Within this file place the following content.

<h3>Latest News</h3> 
<ul>
{% for post in taxonomy.findTaxonomy({'category':['News','news']}) %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
<ul>

Next create another file called blog.taxonomylist.html.twig. Within this file place the following content.

<h3>Latest Blog Posts</h3> 
<ul>
{% for post in taxonomy.findTaxonomy({'category':['Blog','blog']}) %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
<ul>

These are the two files required to filter out the taxonomy’s but we need to tell grav where and when to use them. For this edit the sidebar.html.twig file.

The section within the file which needs editing begins with the line {% if config.plugins.taxonomylist.enabled %} The original section of this file looks like this.

{% if config.plugins.taxonomylist.enabled %} 
<div class="sidebar-content">
<h4>Popular Tags</h4>
{% include 'partials/taxonomylist.html.twig' with {'base_url':new_base_url, 'taxonomy':'tag'} %}
</div>
{% endif %}

Now to change this to use the new files we created earlier and display them when only on the appropriate pages. Edit the above section to look like this.

{% if config.plugins.taxonomylist.enabled %} 
<div class="sidebar-content">
{% if page.route == '/news' %}
{% include 'partials/news.taxonomylist.html.twig' %}
{% elseif page.route == '/blog' %}
{% include 'partials/blog.taxonomylist.html.twig' %}
{% endif %}
</div>
{% endif %}

This configuration takes into account that your addresses match, eg. {% if page.route == ‘/news’ %} is equal to http://www.example.com/news. Also within each post you must label the categories correctly. Within a blog post your frontmatter may look like.

title: 'Creating Independant Taxonomy lists on different pages in Grav' 
published: true
process:
markdown: true
twig: false
taxonomy:
tag:
- grav
category:
- blog

Another trick is to limit the number of recent posts shown. Once again editing blog.taxonomylist.html.twig and changing it from.

<h3>Latest Blog Posts</h3> 
<ul>
{% for post in taxonomy.findTaxonomy({'category':['Blog','blog']}) %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
<ul>

To this

<h3>Latest Blog Posts</h3> 
<ul>
{% for post in taxonomy.findTaxonomy({'category':['Blog','blog']}).order('date', 'desc').slice(0, 30) %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
<ul>

This will limit the number of posts shown on the sidebar to 30 ordered by date in a desc order.

If you haven’t yet tried grav I suggest you head over to the site and look at some of the great features it has on offer.

Source and links


Originally published at fu11circ1e.xyz.