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

Author's articles at the following address

Solved by pamtbaau View solution

Started by Dmitry 6 years ago · 3 replies · 579 views
6 years ago

Hi!

There is a URL /articles

It is necessary to show only the articles of one author in this URL. How can I do it?
Now it works like this:

TWIG
<ul>
    {% for p in page.find('/articles').children %}
        {% for a in p.taxonomy.author %}
            {% if a == author %}
                <li><a href="{{ p.url }}">{{ p.title|e }}</a></li>
            {% endif %}
        {% endfor %}
    {% endfor %}
</ul>

But check {% if page.find('/articles').children %} isn't work correctly. Thanks.

6 years ago

{% set acnt = 0 %}

TWIG
{% set pages = page.find('/articles').children %}
{% for p in pages %}
    {% for a in p.taxonomy.author %}
        {% if a == author %}
            {% set acnt = acnt + 1 %}
        {% endif %}
    {% endfor %}
{% endfor %}
{% if acnt %}

<section class="uk-width-1-2@m articles">
    <h2>{{ 'ARTICLES'|t }}</h2>
    <ul class="uk-list uk-nav-default">
    {% for p in pages %}
        {% for a in p.taxonomy.author %}
            {% if a == author %}
               <li>
                    <a href="{{ p.url }}">{{ p.title|e }}</a>
                </li>
            {% endif %}
        {% endfor %}
    {% endfor %}
    </ul>
</section>
{% endif %}

Now it work. It looks terrible. But work.

6 years ago Solution

@rustark, It seems you want to have a collection of pages which are the children of a specific route, but only if author equals some name. Correct?

Have a look at the documentation on Page Collections.

You can define a collection in the header of the page, like:

YAML
content:
   items:
      '@page.children': '/articles'

or dynamically in Twig:

TWIG
{% set collection = page.evaluate([{'@page.children':'/articles'}]) %}

To address your specific question, the following will create:

  • a collection 'coll1' of all children below '/articles'
  • a collection 'coll2' of all pages with taxonomy of specific author
  • a collection 'pages' which is the intersection of the two yielding all pages below '/article' which have the correct author.
    TWIG
    
    {% set author = 'an author' %}
    {% set coll1 = page.evaluate({'@page.children':'/articles'}) %}
    {% set coll2 = page.evaluate({'@taxonomy': {'author': author}}) %}
    

{% set pages = coll1.intersect(coll2) %}

TXT
The pages contain the following header:

taxonomy:
author: 'an author'

TXT

👍 1
last edited 01/13/20 by pamtbaau
6 years ago

Thanks! It's work.

{% set autor = 'Name' %}

TWIG
{% set articles = page.evaluate({'@page.children':'/articles'}) %}
{% set authors = page.evaluate({'@taxonomy': {'author': author}}) %}
{% set pages = articles.intersect(authors) %}

{% if pages|length > 0 %}
<section>
    <ul>
    {% for p in pages %}
        <li><a href="{{ p.url }}">{{ p.title|e }}</a></li>
    {% endfor %}
    </ul>
</section>
{% endif %}
👍 1

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 15 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