Skip to content
● Grav 2.2 is out: 2x faster cold starts and 96% less memory on big sites. Read the announcement →

Add the text "no posts found" in archives

Started by elisabetta 7 years ago · 7 replies · 830 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 webmasterodp, 1 week ago
4 157 6 days ago
Support · by nwhitfield, 1 week ago
2 136 1 week ago
Support · by stell, 3 weeks ago
5 369 2 weeks ago
Support · by gravinator, 1 month ago
5 559 2 weeks ago
Support · by Gravver, 3 weeks ago
6 347 3 weeks ago