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

Excluding item from collection using categories

Started by Pedro M 4 years ago · 4 replies · 533 views
4 years ago

Hi.

It is possible to show a certain number of posts, according to their category, in a given post, not including the one being viewed.

Example:
POST_BY_CATEGORY|690x471

Thanks

4 years ago

I am using a Gantry particle to display content from Grav. The particle specific code to define the page collection is this:

TWIG
{% set attr_extra = particle.extra | attribute_array %}
{% set article_settings = particle.article %}
{% set filter = article_settings.filter %}
{% set sort = article_settings.sort %}
{% set limit = article_settings.limit %}
{% set start = limit.start + max (0, ajax.start | int) %}
{% set display = article_settings.display %}
{% set collection = grav.page.collection (
     {
         items: {'@ taxonomy.category': filter.categories | split ('')},
         order: {by: sort.orderby, dir: sort.ordering},
         url_taxonomy_filters: false
     },
     false) %}
{% set total = collection.count () %}
{% set pages = collection.slice (start, limit.total) %}

With this code I get this result:

screencapture_jvillen|467x500

You can see the same article on the cards below. I would like that in the article or main post, it does not appear in the lower list of cards.

last edited 01/09/22 by Pedro M
4 years ago

I think you could filter current page out before these two lines

TWIG
{% set total = collection.count () %}
{% set pages = collection.slice (start, limit.total) %}

But there might be a better way. I'm not familiar with Gantry

4 years ago

Hi.

The pages are already filtered before the lines you propose.
I have tried with @ self-siblings on the line:
items: { '@self.siblings': filter.categories | split (' ') }, and I have obtained the desired result, but for this, I must have the pages at the same level, not being able to apply this filter to all the pages of the site, regardless of the level at which they are.

Suggested topics

Topic Participants Replies Views Activity
General · by Hanns Mattes, 9 hours ago
1 8 55 minutes ago
General · by Andy Miller, 1 hour ago
0 4 1 hour ago
General · by Jerry Hunt, 5 days ago
2 136 1 day ago
General · by pamtbaau, 1 day ago
1 93 1 day ago
General · by Andy Miller, 2 days ago
0 77 2 days ago