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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Collection without pages that have a certain header-value

Started by Sascha 3 years ago · 5 replies · 345 views
3 years ago

Is there a way (via frontmatter preferably) to create a collection excluding pages with a certain custom value in the page-header?

TXT
hide_teasers: true

I have some pages (blog-children) that i want to show up only in the rss-feed and not in teasers on the website. I can't get it to work on my blog-index with pagination.

In the template i tried

TWIG
{% set cleaned = page.collection() %}
{% for p in collection %}
    {% if p.header.hide_teasers %}
     {% set cleaned = cleaned.remove(p) %}
    {% endif %}    
{% endfor %}

and then looping through the teasers like

TWIG
{% for child in cleaned %}
    {% include 'partials/blog-list-item.html.twig' with {blog: page, page: child} %}
{% endfor %}

That skips the "hidden" pages but if the limit is 6 and there are 4 hidden pages it only shows 2 teasers of a total of about 20-30 pages.

blog.md frontmatter is set like this atm:

YAML
content:
    items:
        - '@self.children'
    limit: 6
    order:
        by: date
        dir: desc
    pagination: true
    url_taxonomy_filters: true

This page https://learn.getgrav.org/17/content/collections#multiple-collections says, "filter" is not possible with custom header-values. So, how can i achieve what i am looking for?

Any hints or ideas? Thanks in advance.

3 years ago

Maybe you could try something like this:

TWIG
{% set collection = page.collection() %}
{% for child in collection %}
    {% if child.header.hide_teaser == false or child.header.hide_teaser is not defined %}
      {% include 'partials/blog-list-item.html.twig' with {'page':child, 'truncate':true} %}
    {% endif %}
{% endfor %}

Previously, in blueprint hide_teaser would be:

YAML
header.hide_teaser:
    type: toggle
    toggleable: true
    label: Hide Teaser
    highlight: 0
    default: 0
    options:
      1: PLUGIN_ADMIN.ENABLED
      0: PLUGIN_ADMIN.DISABLED
    validate:
      type: bool
3 years ago

Thanks a lot! Didn't know i need to "define" the variable in the blueprint. I guess blog.yaml? Unfortunately it ends up in the same result.
The first page shows only 2 teasers instead of 6.
Let me explain. I have about 30 children of /blog

  1. show teaser
  2. hide
  3. hide
  4. hide
  5. hide
  6. show
  7. show
  8. show
  9. show
  10. show
  11. show
  12. show
  13. show
  14. show
  15. show
  16. hide

what i get on page 1 with limit 6 is:
1.
6.

instead of
1.
6.
7.
8.
9.
10.

Do you understand?

3 years ago

@assbach, Instead of removing one or more items from a collection, you could also create a collection that only contains the items you are aiming at.

  • Add a new taxonomy "outlet" in site.yaml
    TXT
    taxonomies: [category,tag,month, outlet]
    
  • For each blog or rss page set the following in its frontmatter:
    YAML
    taxonomy:
    outlet: [rss, blog]   // visible in blog and rss collection
    --or--
    outlet: [rss]         // visible in rss collection
    --or--
    outlet: [blog]        // visible in blog collection
    
  • Define collection as follows:
    YAML
    content:
    items:
      '@taxonomy.outlet': blog   // Creates collection with blog items
      --or--
      '@taxonomy.outlet': rss    // Creates collection with rss items
    limit: 6
    order:
      by: date
      dir: desc
    pagination: true
    url_taxonomy_filters: true
    

Pagination will be shown correctly.

"Look ma, frontmatter only"

NB:

Didn’t know i need to “define” the variable in the blueprint. I guess blog.yaml?

It's not required if you use a code editor. It's only needed when you want to set the value using Admin.

👍 2
last edited 04/11/23 by pamtbaau
3 years ago

@pamtbaau I was thinking in that option too. 👍 Great

3 years ago

Awesome. That seems to work. Thanks a lot!
Now i only have to find out how i need to change the feed plugin so that it respects these settings.
This way i could publish articles that won't show up in the RSS feed but only on the website. Great for testing as well.

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
3 92 3 hours ago
Support · by Anna, 3 days ago
2 94 23 hours ago
Support · by Justin Young, 1 day ago
1 62 1 day ago
Support · by Duc , 1 week ago
2 98 6 days ago
Support · by Colin Hume, 1 week ago
2 91 6 days ago