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

Add the text "no posts found" in archives

Started by elisabetta 6 years ago · 7 replies · 437 views
6 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!

6 years ago

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

6 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
6 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 %}
6 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).

6 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 ... 😉

last edited 03/16/20 by elisabetta
6 years ago

Editing Twig template files is done outside of Grav by using a text or code editor of your choosing.

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 53 9 hours ago
Support · by Anna, 3 days ago
2 59 12 hours ago
Support · by Justin Young, 13 hours ago
1 30 13 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 56 5 days ago