How To Generate URL Slug From Metaobject?

Solved

How To Generate URL Slug From Metaobject?

webdevbyalex
Shopify Partner
6 0 4

I have a metaobject called "brand" within a metafield "created_by" and I want to use the slug of the "name" key of the brand to create a link to the brand page template. Example link... https://www.beforeourtime.com/pages/brand/colors-greys 

"colors & greys" is the name of a brand. This is the metaobject definition... https://snipboard.io 

This is how I reference the brand name on my product page template... 

{{ product.metafields.custom.created_by.value.name }}

Accepted Solution (1)

Small_Task_Help
Shopify Partner
830 27 75

This is an accepted solution.

Hi,

 

1. Create a Metafield-->Assign a Value(This value could be a string that you want to use as a part of the URL slug.)

2. Generate the slug using the metafield value, you can use liquid code. Here's an example for a product:

 

{% assign product_metafield = product.metafields.namespace.key %}
{% assign slug = product_metafield | handle %}

 

In this code:

product.metafields.namespace.key should be replaced with the actual namespace and key of your metafield.

handle is a filter in liquid that converts a string into a URL-friendly slug. It removes spaces, converts to lowercase, and replaces special characters.

Use the generated Slug in Your URLs

Example

custom product URL

<a href="/products/{{ slug }}">{{ product.title }}</a>

 

If you still need help from our side you can contact us through given details at signature

To Get Shopify Experts Help, E-mail - hi@ecommercesmalltask.com
About Us - We are Shopify Expert Agency
At Google My Business - Ecommerce Small Task - Hire Shopify Developers Ahmedabad

View solution in original post

Replies 2 (2)

Small_Task_Help
Shopify Partner
830 27 75

This is an accepted solution.

Hi,

 

1. Create a Metafield-->Assign a Value(This value could be a string that you want to use as a part of the URL slug.)

2. Generate the slug using the metafield value, you can use liquid code. Here's an example for a product:

 

{% assign product_metafield = product.metafields.namespace.key %}
{% assign slug = product_metafield | handle %}

 

In this code:

product.metafields.namespace.key should be replaced with the actual namespace and key of your metafield.

handle is a filter in liquid that converts a string into a URL-friendly slug. It removes spaces, converts to lowercase, and replaces special characters.

Use the generated Slug in Your URLs

Example

custom product URL

<a href="/products/{{ slug }}">{{ product.title }}</a>

 

If you still need help from our side you can contact us through given details at signature

To Get Shopify Experts Help, E-mail - hi@ecommercesmalltask.com
About Us - We are Shopify Expert Agency
At Google My Business - Ecommerce Small Task - Hire Shopify Developers Ahmedabad
webdevbyalex
Shopify Partner
6 0 4

Thanx Bro!