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.

General

Count the archives items

Solved by Vadym View solution

Started by Pedro M 3 years ago · 4 replies · 304 views
3 years ago

Hi.

I'd like show the archives section in sidebar only if archives have a length > 0, like related pages (in Quark theme for example)

How I filter this in sidebar template?

Thanks.

3 years ago

@pmoreno, are you talking about the future2021 theme?

3 years ago

Hello @b.da

Actually, it is a question related to any theme, but in this case it is about the Editorial theme. This theme has a sidebar like Future2021 and like Quark, where some blocks are located, depending on whether the plugins are installed or not (feeds, relatedpages, archives, taxonomylist, etc).

In this specific case, if the archives plugin is enabled, the text "Archives" appears and below the archives by month. But if you are not on the blog or item page, only the text "Archives" is displayed.

archives|380x154

I would like the "Archives" header to not appear if there are no files to display (just like the RelatedPages plugin does). So far I have only managed to prevent it from being displayed by filtering by template type. Example:

TWIG
{# Show Archives if plugin is enabled #}
         {% if config.plugins.archives.enabled and (page.template == 'blog' or page.template == 'item') %}
             {% include 'partials/archives.html.twig' with {'base_url': new_base_url} %}
         {% endif %}

But I don't know if it's correct to filter it like this.
I have tried something like this:

TWIG
{% if config.plugins.archives.enabled  and archives_show_count|length > 0 %}

But it does not work.

3 years ago Solution

@pmoreno, you can use empty test, or length filter, and check archives_data

TWIG
{% if config.plugins.archives.enabled and archives_data|length > 0 %}
    {% include 'partials/archives.html.twig' with {'base_url': new_base_url} %}
{% endif %}

or

TWIG
{% if config.plugins.archives.enabled and archives_data is not empty %}
    {% include 'partials/archives.html.twig' with {'base_url': new_base_url} %}
{% endif %}

also you must define new_base_url link_1, link_2

3 years ago

@b.da:

TWIG
{% if config.plugins.archives.enabled and archives_data is not empty %}
    {% include 'partials/archives.html.twig' with {'base_url': new_base_url} %}
{% endif %}

Hello @b.da. With this code it works well. My new_base_url was already well defined, I just didn't know which plugin variable archives to compare against.

Thank you very much again. I will add this new feature in the Future2021 and Editorial themes soon.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 80 10 hours ago
General · by pamtbaau, 15 hours ago
1 51 14 hours ago
General · by Andy Miller, 1 day ago
0 45 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago