Filtering content

Hi everyone,
I have two questions about filtering. On the website I’m working on, there is a page with a listing of companies. Now I would like the visitor to be able to filter these companies. At first I wanted use taxonomy categories for this, but since I want the admin to choose from a limited amount of categories, I decided to create a custom categories select input. In the YAML it looks like this:

header.company_category:
  type: select
  label: Category
  multiple: true
  options:
    one: Design
    two: Kunst
    three: ICT
    four: Business
    five: Other
  validate:
    type: array

In the admin this work exactly as I like to, but I wonder if there is a way to use the taxonomy categories and limit the choice the admin has?

The next step is to actually put a filter on the company listing page. By copy-pasting some code from other themes, I tried to make a list like this:

<ul class="filter-menu">
  {% for company_category in page.children.header.company_category %}
    <li>
      <a href="{{ blog.url|rtrim('/') }}/company_category{{ config.system.param_sep }},{{ company_category }}" class="p-category">
        {{ company_category }}
      </a>
    </li>
  {% endfor %}
</ul>

Is this the right approach? If anyone can give me nudge in the right direction, that would be awesome! Thanks :slight_smile:

You can dynamically populate the options using data-options - see https://learn.getgrav.org/forms/blueprints/advanced-features#using-function-calls-data. Build a small plugin (or have a method in your theme main php file) that provides the list of companies.

Thanks for your reply! Reading back my first post; hmm not so clear :slight_smile: Had been staring at the issue for too long, so was a bit numb :).

Anyhow, my main question is: how can I filter the posts on a listing page? By following the instructions in this forumpost, I managed to get a menu with a list of the taxonomies by doing this:

{% set taxlist = taxonomylist.get() %}
{% if taxlist %}
  {% set category = grav.uri.params("category", true) %}
  {% for tax,value in taxlist['category'] %}
    {% set current_page = (tax == category) ? 'active' : '' %}
    <li class="{{ current_page }}">
      <a href="{{ base_url }}/category{{ config.system.param_sep }},{{ tax|e('url') }}">{{ tax|capitalize }}</a>
    </li>
  {% endfor %}
{% endif %}

But now, when I click the links (http://localhost:8888/category:design), no posts are shown… What do I need to do to make this work?

Cheers!
Ernest

ps. Is there no way to let an admin select from a set of categories without building a plugin?

Nevermind! I finally found it! :smiley: It’s so much easier than I thought. Phew :slight_smile:

Hey what was your solution? Having the same issue filtering posts on a listing page. Cheers

Pfieuw, that’s a while ago. I don’t exactly remember my thought path anymore. What I did is this:

{% set taxlist = taxonomylist.get() %}
{% if taxlist %}
  {% set category = grav.uri.params("category", true) %}
  {% for tax,value in taxlist['category'] %}
    {% set active = uri.param('category') == tax ? 'active' : '' %}

    <a class="{{ active }}" href="{{ page.url }}/category{{ config.system.param_sep }},{{ tax|e('url') }}#filter-desktop">
      {{ tax|capitalize }}
    </a>
  {% endfor %}
{% endif %}

I hope this helps you in getting it to work. If not, let me know. Then I’ll dig some deeper