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!
Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025