May 22, 202410:18 AM - edited May 22, 202410:34 AM
Member
I wanna know how to sort blog articles in descending order.
SOLVE
Hello! I would like to sort my blog articles in descending order, that is, in order of oldest posting date. I edited bolog-listings module file as below, but they don't work.
{% for c in contents |sort(attribute='publish_time') %}
{% if loop.index0 < remaining_cols %}
{# initial columns #}
{% do initial_posts.append(c) %}
{% else %}
{# regular columns #}
{% do remaining_posts.append(c) %}
{% endif %}
{% endfor %}
{% for c in contents |reverse %}
{% if loop.index0 < remaining_cols %}
{# initial columns #}
{% do initial_posts.append(c) %}
{% else %}
{# regular columns #}
{% do remaining_posts.append(c) %}
{% endif %}
{% endfor %}
How should I edit the module file? thanks in advance.
May 22, 202410:50 AM - edited May 22, 202411:16 AM
Contributor
I wanna know how to sort blog articles in descending order.
SOLVE
For the reverse, try removing the space before the |Reverse. This will only reverse the order of posts displayed on the current page. If you have pagination, then it won't sort through the other pages.
{% for c in contents|reverse %}
<article class="index-article">
<div class="blog-listing">
{% for tag in post.tag_list %}
{% if loop.index == 1 %}
<h2>{{ tag.name }}</h2>
{% endif %}
{% endfor %}
</div>
<div class="Listing-content">
<p class="title"><a href="{{post.absolute_url}}">{{post.name}}</a></p>
<p class="exerpt">{{ post.meta_description }}</p>
</div>
</article>
{% endfor %}
May 22, 202410:50 AM - edited May 22, 202411:16 AM
Contributor
I wanna know how to sort blog articles in descending order.
SOLVE
For the reverse, try removing the space before the |Reverse. This will only reverse the order of posts displayed on the current page. If you have pagination, then it won't sort through the other pages.
{% for c in contents|reverse %}
<article class="index-article">
<div class="blog-listing">
{% for tag in post.tag_list %}
{% if loop.index == 1 %}
<h2>{{ tag.name }}</h2>
{% endif %}
{% endfor %}
</div>
<div class="Listing-content">
<p class="title"><a href="{{post.absolute_url}}">{{post.name}}</a></p>
<p class="exerpt">{{ post.meta_description }}</p>
</div>
</article>
{% endfor %}
I wanna know how to sort blog articles in descending order.
SOLVE
Thank you for answering. I tried the code below first, but the ascending order remained the same. Please let me know if there is anything else that needs to be corrected.😭
{% for c in contents|sort(True, False, "publish_date") %}
{% if loop.index0 < remaining_cols %}
{# initial columns #}
{% do initial_posts.append(c) %}
{% else %}
{# regular columns #}
{% do remaining_posts.append(c) %}
{% endif %}
{% endfor %}