{{page.url}} is first post and not current pages URL

I’m currently designing a language switcher on my Jekyll site. Basically, it’s just 2 links for the 2 supported languages. When I click a link, I want it to redirect to the current page in the target language.

When I use {{ page.url }}, it always displays a link to the first post I created, not the current page. It doesn’t matter, whether it’s a post or a page, or if it’s the current language or not.

What’s the problem that it always uses the first post? And how can I fix it, so I can link to the current page?

is this part of a loop? what does the code look like? directory structure?

No, it shouldn’t be part of a loop.

I have a file _layouts/default.html where I include this file with {% include_cached masthead.html %}, just after the HTML body tag. In there, I wanted to use {{ page.url }} to get the current URL of the page, since this gets used by all pages and posts.

I’m quite new to Jekyll, so I don’t know much about it. If it helps, my theme is minimal-mistakes. For localization, I use the jekyll-polyglot plugin.

so if you do page.url in the actual page you get a different value than the one that is in the include called by the layout?

Maybe this is an issue with includes be called from a layout and you always get the same page? not sure but that could be it. There are some limits to what you can do as things get processed in a certain order.

Try looking into passing parameters in the include - and then see if you can pass in the page.url from the layout into the include.

Looks like you would need capture to get the value of page.url:

Thank you for the idea about passing the variable in!

Just as I wanted to edit the include, I recognized that it uses the cached layout:

{% include_cached masthead.html %}

Because of that, I just wanted to try it with only using {% include masthead.html %}, which worked correctly.

Your approach by passing in the variable would still work:


{% assign page_url = page.url %}
{% include_cached masthead.html page_url=page_url %}


<a href="{{ page_url }}"></a>