Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hello,
I did ask this in a different topic, but I think it is more applicable in this topic.
I am new to Liquid, however, I have been searching the docs, and I am still having some trouble.
What would the syntax look like for sorting metafields by their unit of measurement? I have the metafields I want to display in the namespace specs..
Here is the current liquid I have written:
{% if product_specs.size > 0 %}
<div id="specifications">
<ul>
{% for spec in product.metafields.specs -%}
<li><strong>{{ spec.first | capitalize }}</strong>: {{ spec.last.value }}</li>
{%- endfor %}
</ul>
</div>
{% endif %}
Here is the output:
Height: 55 mm
Weight: 2 oz
Width: 25 mm
So, I want Height and Width displayed next to each other, any thoughts or help on this would be greatly appreciated!
Hi @lamez ,
Welcome to the Shopify community 🙂
While Liquid does have some tricks for sorting arrays, I'm afraid there's no default solution to your specific problem. However, you can do that by applying conditional logic to your code as shown here. Or you can add numeric indexes to metafield keys and let Liquid do the job for you. Once the array is sorted, you can simply strip those indexes from the keys prior to displaying them on the product page.
@MetafieldsGuru thank you for your response, giving the examples of the syntax, and the idea of adding in an index is extremely helpful!
Thank you so much!
By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024