PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, June 30, 2022

[FIXED] How to prevent duplicate blog post dates in shopify

 June 30, 2022     article, date, shopify     No comments   

Issue

I have a blog with posts on a shopify website. I want to get all the publication dates of posts, and I do it like this:

{% for article in blog.articles %}
   {{ article.created_at }}                 
{% endfor %}

And this is the output result:

2022-02-01 14:50:06
2022-01-25 05:25:33
2022-01-25 05:24:48

The result shows that there are two posts with the date 2022-01-25 and one post with the date 2022-02-01. But all these dates have different seconds and minutes.

I want to remove seconds and minutes by setting the format to date: "%B %Y":

{{ article.created_at | date: "%B %Y" }}

And in the end I get this result:

February 2022
January 2022
January 2022

Now the question is: How do I prevent duplicate dates with the same format date: "%B %Y"?

I can easily solve this problem with javascript, but I want to know if it's possible to solve this problem using only shopify features.

I will be glad to any answer. Thanks!


Solution

You can store all the dates in as string

{%- capture dates -%}
{%- for article in blog.articles -%}
   {{ article.created_at | date: "%B %Y" }},            
{%- endfor -%}
{%- endcapture -%}

And split and uniq the values to get only the unique ones:

{% assign unique_dates = dates | split: ',' | uniq %}

This will remove any values that are repeating.

Hope this is what you are looking for.



Answered By - drip
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing