Skip to content
Grav 2.0 is officially stable. Read the announcement →
General

Create collection independent of query params in url

Solved by pamtbaau View solution

Started by Pedro M 5 years ago · 4 replies · 652 views
5 years ago

Hi again

Related to my post Link to post by author...

If I now click on the author's link, the minispost block, based on a collection of pages by taxonomy, changes the links to the posts and only shows those of the selected author.
I am using the Future Imperfect theme, and the code related to this behavior is as follows:

miniposts.html.twig
....

TWIG
{% set miniposts = page.collection ({'items': {' @ taxonomy ': {' category ':' minipost '}},' order ': {' by ':' date ',' dir ':' desc '}}). remove (page) .slice (0,3)%}
{% if count (miniposts)> 0%}

....

blog_item.html.twig
....

TWIG
<a href="{{ base_url }}/author{{ config.system.param_sep }}{{ author }}" itemprop="keywords" class="author"><span class="name" rel="author">{{ author }}</span><img src="{{ page.media[page.header.avatarImage].url|e}}" alt="{{ author }}" /></a>

...

You can see this behavior at the following address:

http://future.juanvillen.es/

What should I modify to show them in the sidebar of miniposts, regardless of whether the link to the authors is clicked or not?

last edited 10/26/21 by pamtbaau
5 years ago Solution

@pmoreno, By default, page.collection uses setting 'url_taxonomy_filters': true, which means the collection will be filtered by the query provided in the url.

When using url /blog/author:foo, the collection will be filtered on 'foo'.

To prevent this automatic filtering, you can set 'url_taxonomy_filters': false.

TWIG
{% for p in page.collection(
   {
      'items': {'@taxonomy': {'category':'blog'}}, 
      'url_taxonomy_filters': false
   }
).slice(0,3) %}

Or you can create a collection using:

TWIG
{% for p in page.evaluate({'@taxonomy.category':'blog'}).slice(0,3) %}

Docs:

5 years ago

Maybe I didn't explain myself well in the last post. What I need is that the three posts highlighted in the left sidebar are always the same (identified with the taxonomy of 'miniposts'). What happens is that when you click on the link of the author of any post, the highlighted miniposts on the left sidebar change and those corresponding to the author that has been clicked are displayed.

Can't you have the two collections independently?

I leave the complete code of the blog_item.html.twig file and of minipost.html.twig.

blog_item.html.twig

TWIG
<article class="box post post-excerpt"  itemprop="liveBlogUpdate" itemscope itemtype="http://schema.org/BlogPosting">
         {% if page.header.author %}
         {% set author =  page.header.author %}
         {% else %}
         {% set author =  page.header.taxonomy.author[0] %}
         {% endif %}
         {% if author %}
         {% set avatar = author|replace(' ', '-')|lower %}
         {% endif %}
    {% if site.share.facebook or site.share.twitter %}
    <script type="text/javascript">
    {% if site.share.twitter %}
    jQuery.getJSON('https://cdn.api.twitter.com/1/urls/count.json?url={{ page.url(true) }}/&callback=?', function (data) {
        jQuery('#so-url-shares{{ loop.index }}').text(data.count);
    });
    {% endif %}
    {% if site.share.facebook %}
    jQuery.getJSON('http://graph.facebook.com/?id={{ page.url(true) }}', function (data) {
        jQuery('#so-fburl-shares{{ loop.index }}').text(data.shares);
    });
    {% endif %}
    </script>
    {% endif %}
    <header>
    <div class="title">
        {% if page.header.link %}
            <h2 itemprop="headline">
                {% if page.header.continue_link is not sameas(false) %}
                    <a href="{{ page.url }}"></a>
                {% endif %}
                <a href="{{ page.header.link }}">{{ page.title }}</a>
            </h2>
            {% else %}
            <h2 itemprop="headline"><a href="{{ page.url }}">{{ page.title }}</a></h2>
        {% endif %}
        {% if page.header.description %}
        <p itemprop="alternativeHeadline">{{ page.header.description }}</p>
        {% endif %}
    </div>
    <div class="meta">
        <time class="published" itemprop="datePublished" datetime="{{page.date|date("Y-m-d")}}">{{ page.date|date("M j, Y")}}</time>
        {% if author %}
           <a href="{{ base_url }}/author{{ config.system.param_sep }}{{ author }}" itemprop="keywords" class="author"><span class="name" rel="author">{{ author }}</span><img src="{{ page.media[page.header.avatarImage].url|e}}" alt="{{ author }}" /></a>
        {% endif %}
    </div>
    </header>

        {% if big_header %}
            {{ page.media.images|first.cropResize(1038,437).html(page.title, page.title, 'image featured') }}
        {% else %}
            {{ page.media.images|first.cropZoom(1038,437).html(page.title, page.title, 'image featured') }}
        {% endif %}

    <div itemprop="articleBody">

    {% if page.header.continue_link is sameas(false) %}
        {{ page.content }}
        {% if not truncate %}
        {% set show_prev_next = true %}
        {% endif %}
    {% elseif truncate and page.summary != page.content %}
        {{ page.summary }}
    {% elseif truncate %}
        {{ page.content|truncate(550) }}
    {% else %}
        {{ page.content }}
        {% set show_prev_next = true %}
    {% endif %}

   </div>

    <footer>
    <ul class="actions">
    {% if truncate and page.summary != page.content %}
        <li><a href="{{ page.url }}" class="button">Continue Reading</a></li>
    {% elseif truncate %}
        <li><a href="{{ page.url }}" class="button">Continue Reading</a></li>
    {% endif %}

    {% if show_prev_next %}
           {% if not page.isLast %}
                <li><a class="button" href="{{ page.prevSibling.url }}"><i class="fa fa-chevron-left"></i> Previous Post</a></li> 
            {% endif %}
           {% if not page.isFirst %}
                <li><a class="button" href="{{ page.nextSibling.url }}">Next Post <i class="fa fa-chevron-right"></i></a></li>         
            {% endif %}

    {% endif %}

    </ul>
    <ul class="stats">
            {% if page.taxonomy.tag %}
                {% for tag in page.taxonomy.tag %}
        <li><a href="{{ base_url }}/tag{{ config.system.param_sep }}{{ tag }}" itemprop="keywords">{{ tag }}</a></li>
                    {% endfor %}
            {% endif %}
        <li><a href="https://twitter.com/share" data-url="{{ page.url(true) }}" data-text="{{ page.title }}" class="icon fa-heart">
            <span id="so-url-shares{{ loop.index }}"><i class="fa fa-circle-o-notch fa-spin"></i></span></a></li>
        <li><a href="http://www.facebook.com/sharer.php?u={{ page.url(true) }}" class="icon fa-comment"><span id="so-fburl-shares{{ loop.index }}">0</span></a></li>
    </ul>
</footer>

</article>

miniposts.html.twig (this is inserted in base.html.twig)

TWIG
{% set miniposts = page.collection({'items':{'@taxonomy':{'category': 'minipost'}},'order': {'by': 'date', 'dir': 'desc'}}).remove(page).slice(0,3) %}
{% if count(miniposts) > 0 %}
<section>
    <header class="">
        <h3>Featured</h3>
    </header>
    {% for p in miniposts %}
    <article class="mini-post">
        <header>
            <h3><a href="{{ p.url }}">{{ p.title }}</a></h3>
            <time class="published" datetime="2015-10-20">{{ p.date|date("M j, Y")}}</time>
            <a href="{{ p.url }}" class="author"><img src="{{ p.media[p.header.avatarImage].url|e}}" alt="" /></a>
        </header>
        <a href="{{ p.url }}" class="image"><img src="{{ p.media[p.header.primaryImage].url|e}}" alt="" /></a>
    </article>
    {% endfor %}

</section> <!-- End of featured Section -->
{% endif %}
5 years ago

@pmoreno, Have you tried my suggestions for your minipost collection?

last edited 10/26/21 by pamtbaau
5 years ago

Hello
If I considered your solution and I've managed the solution with

TWIG
{% for p in page.evaluate({'@taxonomy.category':'minipost'}).slice(0,3) %}

Now I have managed to do what I needed, and that is that in the sidebar of miniposts, they remain fixed, regardless of whether you click on the links of the authors of the posts (which previously made that in the sidebar only the links appear of those authors).

Another code that I have tried is the following:

TWIG
{% set miniposts = page.find('/blog').children.order('header.taxonomy.category|minipost','date','desc').slice(0, 3) %}

I have mixed a search on blog pages for a specific taxonomy.

Thanks for your help.

last edited 10/26/21 by Pedro M

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 133 24 hours ago
General · by pamtbaau, 1 day ago
1 92 1 day ago
General · by Andy Miller, 2 days ago
0 77 2 days ago
General · by Marcel, 12 months ago
6 386 5 days ago
General · by Duc , 6 days ago
3 71 6 days ago