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

Page.find plus a for loop not showing all children pages

Solved by pamtbaau View solution

Started by Marco Cevoli 1 year ago · 4 replies · 52 views
1 year ago

I have a special page showing an archive of all blog posts and all articles for each tag. I created it with the following code (with the help of Copilot):

TWIG
{% set posts = page.find('/blog').children.published.order('header.date', 'desc') %}
{% set post_count = 0 %}
{% for post in posts %}
    {% set post_count = post_count + 1 %}
{% endfor %}

[Edited text...] **{{ post_count }} articles** [edited text].

<h2>Post per mese</h2>

{# Calcola l'anno e il mese correnti #}
{% set currentYear = "now"|date("Y")|int %}
{% set currentMonth = "now"|date("n")|int %}

{# Definisci la mappa dei mesi con abbreviazioni italiane #}
{% set monthNamesIt = {
    1: 'gen', 2: 'feb', 3: 'mar', 4: 'apr', 5: 'mag', 6: 'giu',
    7: 'lug', 8: 'ago', 9: 'set', 10: 'ott', 11: 'nov', 12: 'dic'
} %}

{% set monthNamesEn = {
    1: 'jan', 2: 'feb', 3: 'mar', 4: 'apr', 5: 'may', 6: 'jun',
    7: 'jul', 8: 'aug', 9: 'sep', 10: 'oct', 11: 'nov', 12: 'dec'
} %}

{# Calcola il numero di articoli per ciascun mese #}
{% set articles_per_month = {} %}
{% for post in page.find('/blog').children %}
  {% set postYear = post.date|date("Y") %}
  {% set postMonth = post.date|date("n") %}
  {% set key = postYear ~ '-' ~ postMonth %}
  {% if articles_per_month[key] is not defined %}
    {% set articles_per_month = articles_per_month|merge({ (key): 1 }) %}
  {% else %}
    {% set articles_per_month = articles_per_month|merge({ (key): articles_per_month[key] + 1 }) %}
  {% endif %}

{% endfor %}

{# Ciclo per ogni anno dall'anno corrente fino al 2016 (inizia da ottobre 2016) #}
{% for year in range(currentYear, 2016, -1) %}
  <div class="year-header">
    <h3>{{ year }}</h3>
  </div>
  <div class="month-links">
    {# Definiamo il range dei mesi per ciascun anno: #}
    {% if year == 2016 %}
      {% set monthsRange = [10, 11, 12] %} {# Solo ottobre, novembre e dicembre per il 2016 #}
    {% elseif year == currentYear %}
      {% set monthsRange = range(1, currentMonth) %} {# Solo fino al mese corrente per l'anno attuale #}
    {% else %}
      {% set monthsRange = range(1, 12) %} {# Tutti i mesi per gli altri anni #}
    {% endif %}

    {# Itera sui mesi e non aggiunge "/" dopo l'ultimo mese #}
{% for month in monthsRange %}
  {% set key = year ~ '-' ~ month %}
  {% set article_count = articles_per_month[key] ?? 0 %}
  {% if article_count > 0 %}
    {% set url = "/blog/archives_month:" ~ monthNamesEn[month] ~ "_" ~ year %}
    {% set text = monthNamesIt[month]|upper %}
    <a href="{{ url }}">{{ text }}</a> <span class="label label-rounded">{{ article_count }}</span>{% if not loop.last %} / {% endif %}
  {% else %}
    {% set text = monthNamesIt[month]|upper %}
    <span>{{ text }}</span>{% if not loop.last %} / {% endif %}
  {% endif %}
{% endfor %}

  </div>
{% endfor %}

<h2>Post per tag</h2>

{% set tags = {} %}
{# Filtra i post solo pubblicati e visibili #}
{% set posts = page.find('/blog').children.published.order('header.date', 'desc') %}

{# Raggruppa i post per tag, mantenendo l'ordine dei post (già discendente) #}
{% for post in posts %}
  {% if post.taxonomy.tag is defined %}
    {% for tag in post.taxonomy.tag %}
      {% if tags[tag] is not defined %}
        {% set tags = tags|merge({ (tag): [post] }) %}
      {% else %}
        {% set tags = tags|merge({ (tag): tags[tag]|merge([post]) }) %}
      {% endif %}
    {% endfor %}
  {% endif %}
{% endfor %}

{# Itera sui tag in ordine alfabetico #}
<div class="accordion">
  {% for tag in tags|keys|sort %}
    <div class="accordion-item">
      <input type="checkbox" id="accordion-{{ loop.index }}" hidden>
      <label for="accordion-{{ loop.index }}" class="accordion-header">
        <strong>{{ tag }}</strong> <span class="label label-rounded">{{ tags[tag]|length }}</span>
      </label>
      <div class="accordion-body">
        <ul>
          {# Elenco articoli per il tag corrente #}
          {% for post in tags[tag] %}
            <li><a href="{{ post.url }}">{{ post.title }}</a></li>
          {% endfor %}
        </ul>
      </div>
    </div>
  {% endfor %}
</div>

You can see the page here: https://marcocevoli.com/archivio (in Italian).

I don't understand why the number of posts (the grey one) is displayed correctly, but if you click on the tag, the actual post titles displayed for the "newsletter" tag are 35 instead of 130...

If I debug with a simple loop, I can see the titles are indeed found.

I've looked almost everywhere and I can't find any set limit...

What does this depend on? Grav code? The page.find function? The taxonomy plugin? The underlying PHP code? Some PHP variable on the server? A bug in the actual page twig?

TIA

last edited 03/07/25 by Marco Cevoli
1 year ago

yes, that's the correct number. But if you click, you'll see only 35 titles when the accordion opens. That's the issue. Why 35? If I change tag to another tag, the title shows up. So it doesn't depend on the page. It depends on something else...

1 year ago Solution

@marcocevoli, A quick look at the DOM reveals that all titles are in the DOM. It is the max-height: 50rem (in combination with overflow: hidden;) that limits the number of titles that can be shown.

last edited 03/07/25 by pamtbaau
1 year ago

NOOOOO WAY.... Thanks a lot. I've looked at all possible settings, and it didn't occur to me to inspect the page with F12... so silly of me.

Thanks a million.

Regards,
Marco

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 54 12 hours ago
Support · by Anna, 3 days ago
2 60 15 hours ago
Support · by Justin Young, 16 hours ago
1 30 15 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 57 5 days ago