Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Filtering content

Started by Muut Archive 9 years ago · 5 replies · 646 views
9 years ago

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:

YAML
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:

TWIG
<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 :)

9 years ago

Thanks for your reply! Reading back my first post; hmm not so clear :) 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:

TWIG
{% 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?

9 years ago

Nevermind! I finally found it! :D It's so much easier than I thought. Phew :)

9 years ago

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

9 years ago

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

TWIG
{% 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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1330 9 years ago
Archive · by Muut Archive, 9 years ago
2 922 9 years ago
Archive · by Muut Archive, 9 years ago
2 4051 9 years ago
Archive · by Muut Archive, 9 years ago
1 2930 9 years ago
Archive · by Muut Archive, 9 years ago
3 1108 9 years ago