get current url in liquid file

Highlighted
New Member
4 0 0

Hello,

I'm trying to get the current url in the liquid file, so I can change the content depending on the url.

I tried to work around with content_for_header, but I noticed that the variable caches the information for a short moment. Is there another way? Or is there a way to disable the caching?

/ Paul

0 Likes
Highlighted
Pathfinder
112 1 31

Hey Paul, you are looking for the page.url tag. Refenced here: https://help.shopify.com/themes/liquid/objects/page#page-url

There are also a couple of other tags:

shop.url
shop.domain
collection.url
product.url
blog.url
article.url
article.comment_post_url
canonical_url

The easiest way to get Shopify help. Submit your task, get a quote, and we'll get the work done. Work with me at shupkeep.com
1 Like
Highlighted
New Member
4 0 0

Hey Alex,

that is interesting. I'm trying it on my main page and it displays nothing. Same thing with page.title whis is not empty.

I tried to check If I'm not crazy that code and it prints "blank":

{% if page.title != blank %}
	{{ page.title }}
{% else %}
	blank
{% endif %}

 

0 Likes
Highlighted
Pathfinder
112 1 31

Try changing blank to nil

{% if page.title != nil %}

or you can check against a blank string:

{% if page.title != "" %}

or you can just test if page.title returns anything, which is a truthy check. This is probably the most concise way of doing what you want to do:

{% if page.title %}

Use this as a reference for liquid, it's my favorite resource: https://www.shopify.ca/partners/shopify-cheat-sheet

The easiest way to get Shopify help. Submit your task, get a quote, and we'll get the work done. Work with me at shupkeep.com
1 Like
Highlighted
New Member
4 0 0

I forgot to answer ... thanks for the cheat sheet :)

{% if page.title %}

still doesn't work. The if condition doesn't catch. It looks like that the page variable only works on subpages and not on the main page.

0 Likes
Highlighted
Tourist
7 0 0

Hey,

 

Is there any way to detect or compare specifically if it was a policy/legal page?

 

I'm adding an age-verification, but want to exclude it from showing on the store legal/policy pages.

0 Likes
Highlighted
Shopify Partner
15 0 7

@dhruveonmars You can use shopify request object

 

on your theme.liquid 

 
 
{% if request.path contains '/policies/' %}
 // do some modification!
{% endif %}
 
we use /policies/ for the condition to make sure its the actual policy page and not just a page with a policy handle

Leysam | The Shopify Guy  
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - Hire me full time or per hour :)


Email me at edwardwindtalker@gmail.com or add me on Skype: samypogs
3 Likes
Shopify Partner
153 16 15

Hi @Leysam,

Thanks a lot man. I was working on a custom sidebar for a client and the client wants that the link in the sidebar would stay highlited if the visitors vists the page in the link. I just copied your code and it worked like a charm. Thanks again.

Want to get customization to your store or bug fixing quickly? Hire Me for an hour for only $20 (Or get a fixed quote)
Hire Me For
  • Store Setup
  • HTML, CSS, JS, JQuery and Liquid coding
  • Bug Fixing

Email at umairabbascs@gmail.com or WhatsApp

0 Likes
Highlighted
Excursionist
50 0 3

How to write specific code only for main page.

I tried {{ page.title}}.But it is not returning empty value.How to capture URL For main page.

our shopify  URL  is gullylivefast.com

0 Likes
Highlighted
Shopify Partner
15 0 7

@Akhilkotha96 Could you further explain your problem? 

Leysam | The Shopify Guy  
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - Hire me full time or per hour :)


Email me at edwardwindtalker@gmail.com or add me on Skype: samypogs
0 Likes