Check if current page is homepage on Jekyll
Asked Answered
W

3

36

There's any way to check if the current page is the homepage?

I want use h1 tag for the logo image only when the current page is the website base url.

Weimaraner answered 8/3, 2015 at 22:31 Comment(0)
S
37

You can use page.url to check if the current page is your index page:

{% if page.url == "/index.html" %}
   <h1>...</h1>
{% endif %}
Stingy answered 9/3, 2015 at 2:26 Comment(4)
I used {% if page.layout == 'home' %} but your answer is correct too. Thanks!Weimaraner
I ended up using if {% if page.url == "/" %} . The rest disn't work for me.Byran
Looks like in the version of Jekyll I just installed (3.3.1) the page.url for index.md is '/'. For example: {% if page.url == "/" %}active{% endif %}Axes
If permalink option is set at Front Matter of the page, page.url should be compared to its value. Otherwise, as said above, page.url == "/" works fine (v3.4.3).Andrey
G
11

Another option to manage this is adding page IDs to the yml frontmatter

{% if page.id == 'index' %}
  content
{% endif %}
Gonophore answered 16/12, 2016 at 21:18 Comment(0)
B
1

Along @Christopher's comment the best is if you test the page.layout. Because if permalink is set or in other circumstances the page.url can be "/index.html", "/index", or just simply "/". Therefore I think it's more robust:

{% if page.layout == 'home' %}
  <h1>logo</h1>
{% else %}
  <h2>smaller logo</h2>
{% endif %}
Bechtel answered 6/8, 2021 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.