CMS Development

MM12
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.  

0 Upvotes
1 Accepted solution
pm8rsh88
Solution
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 %}

 

 For sort, try

HubL filters (hubspot.com)

 

{% for c in contents|sort(True, False, "publish_date") %}

{% endfor %}

 

 

View solution in original post

0 Upvotes
5 Replies 5
pm8rsh88
Solution
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 %}

 

 For sort, try

HubL filters (hubspot.com)

 

{% for c in contents|sort(True, False, "publish_date") %}

{% endfor %}

 

 

0 Upvotes
MM12
Member

I wanna know how to sort blog articles in descending order.

SOLVE

Thank you for answering.
The"|Reverse" simply reverses the order of the pages. It was helpful. thank you.

0 Upvotes
pm8rsh88
Contributor

I wanna know how to sort blog articles in descending order.

SOLVE

Aye. The sort method is probably your best bet. See the example in my previous post and the link I provided, and let me know how you get on. 

0 Upvotes
evaldas
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

I wanna know how to sort blog articles in descending order.

SOLVE

Hi @MM12 , 

 

To control the order of how the posts are displayed, you can use the sort filter:

 

{% for c in contents|sort(True, False, "publish_date") %}

 

The very first parameter indicates whether the order needs to be reveresed.

 

Hope this helps!


✔️ Did this post help answer your query? Help the community by marking it as a solution.

0 Upvotes
MM12
Member

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 %}

 

0 Upvotes