Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

Add the text "no posts found" in archives

Started by elisabetta 7 years ago · 7 replies · 768 views
7 years ago

Hi all. I'd like to add the text "no posts found" in my multiple blogs when no posts are present on a certain month.
I use the standard "Archives" plugin by TeamGrav.
I think i have to write a {% if %} condition in the following code:

TWIG
<ul class="archives">
{% for month,items in archives_data %}
 <li>
 <a href="{{ page.url }}/{{ config.plugins.archives.taxonomy_names.month }}{{ config.system.param_sep }}{{ month|date('M_Y')|lower|e('url') }}">
{% if archives_show_count %}
<span class="label">{{ items|length }}</span>
{% endif %}
 <span class="archive_date">{{ month|date(config.plugins.archives.date_display_format) }} </span>
 </a>
</li>
{% endfor %}
</ul>

My problem is I'm no coder, so I need same help in writing the correct {% if %} condition for missing posts and pasting it in the above code, something like

TWIG
{% if items = 0 %}
<p>No posts found for this month.</p>
{% endif %}

Thanks in advance!

7 years ago

Anyone can help?? i couldn't find a reply to my problem here or in gravdiscord chat..

7 years ago

@maria, Having taken a look at the Archives plugin, I get the impression that the array 'archives_data' only contains year/month combinations for which at least 1 item exist.

That means that {% if (items|length) == 0 %} never happens...

last edited 03/16/20 by pamtbaau
7 years ago

You are using an assignment instead of a conditional statement. Small typo, big effect 🙂

Simply change

TWIG
{% if items = 0 %}

into

TWIG
{% if items == 0 %}
7 years ago

@bleutzinn, 'items' is an array, so the correct syntax should be:

TWIG
{% if (items | length) == 0 %}
  ...
{% endif %}

The syntax {% if items = 0 %} would have thrown an error:

0 - Unexpected token "operator" of value "=" ("end of statement block" expected).

1 reply
7 years ago

Thank you so much for correcting my "If" code. Now a further question: how can I alter the default archives.htm.twig file to add the "if" condition? Coming from Joomla (completely different environment!!) my learning curve especially about twig s painfully slow ... 😉

1 reply
last edited 03/16/20 by elisabetta

Suggested topics

Topic Participants Replies Views Activity
Support · by blu3prince, 3 days ago
1 65 1 hour ago
Support · by complanar, 6 days ago
6 156 1 day ago
Support · by Rick Beebe, 7 days ago
2 115 3 days ago
Support · by alex, 5 days ago
2 101 4 days ago
Support · by Marcel, 2 weeks ago
1 151 4 days ago