JonECM
August 30, 2021, 1:04am
1
Hey there,
Is there a simple way to change the color (and the font-weight) of just the FIRST word of a product title.
Example: SALE Product Title
Two solutions I tested:
I know this can be achieved with :before and product tags, which is a good solution but for SEO, Google Shopping etc. the SALE is required to be in the name.
I also added a div to the product title directly but this causes issues with the cart.
I was thinking of some Array or other filters (https://shopify.dev/api/liquid/filters/array-filters ) but I don’t know them well enough to work with it.
Thanks!
Jon
@JonECM
Sorry you are facing this issue, it would be my pleasure to help you.
Welcome to the Shopify community!
Thanks for your good question.
Please share your site URL,
I will check out the issue and provide you a solution here.
JonECM
August 30, 2021, 12:23pm
3
Hey there,
thank you! We could just use any Shopify store as this can be applied to any.
Here an example with MVMT: https://www.mvmt.com/mens-watches/chrono/chrono-gunmetal-sandstone/MC01GML.html
So how to make the first one word in the title red (and bold):
CHRONO GUNMETAL SANDSTONE
Thanks!
JonECM
August 30, 2021, 12:39pm
5
@JonECM
sorry its not shopify store
JonECM
August 30, 2021, 12:51pm
7
JonECM
August 30, 2021, 1:24pm
9
That’s amazing, thank you! Is there a non JS solution? Like I first suggested with using SPLIT filters: https://shopify.dev/api/liquid/filters/array-filters
JonECM
August 30, 2021, 2:35pm
11
Again, looking for a non JS solution.
Hi @JonECM ,
You can add the following code:
{%- assign titleArray = product.title | split: " " -%}
{{ titleArray[0] }}
{{ product.title | remove_first: titleArray[0] }}
Then you have the split text, you can add html as you like. Hope it helps!
Please mark as a solution if you find my answer helpful for you.
JonECM
August 30, 2021, 2:58pm
13
Perfect! One last question, how would I edit the HTML? Sorry I have no idea about this array stuff. CSS and HTML is fine.
This is what I added now to only apply this to items on sale. And now I want to have the first word (which is SALE) in red and bold.
{%- if product.tags contains 'sale' -%}
{%- assign titleArray = product.title | split: " " -%} {{ titleArray[0] }} {{ product.title | remove_first: titleArray[0] }}
{%- else -%}
Hi @JonECM ,
Please change the code:
{{ titleArray[0] }} {{ product.title | remove_first: titleArray[0] }}
It will work fine.
Hope it helps!
You can add the following code: {%- assign titleArray = product.title | split: " " -%} {{ titleArray[0] }} {{ product.title | remove_first: titleArray[0] }} Then you have the split text, you can add html as you like. Hope it helps!