How to check if current page is homepage?

I want to run some Hugo template code on the homepage.

I use pagination and that gives me 5 index pages:

  • example.com/
  • example.com/2/
  • example.com/3/
  • example.com/4/
  • example.com/5/

I want to run code only on the first of those. (That’s the actual homepage for me.) But how can I check that conditionally?

Page variables that I’ve tried:

  • .IsHome; returns true on each of the five pages.
  • .RelPermalink; is / on each page.
  • .Kind; is home on each page.
  • .IsNode; always true for each page.
  • .Permalink; always the same on each page.

Hmm. :thinking: Anyone got an idea? :slightly_smiling_face:

maybe:

{{ if and .IsHome ( eq $paginator.PageNumber 1 ) }}
  yo
{{ end }}

Thanks for the quick reply!

Unfortunately I’m also looking to check if a page is the homepage outside my pagination partial.

So $paginator isn’t available. And through .Paginator I can’t access the page number.

1 Like

:thinking:

You got code I can play with?

Unfortunately not, it’s a private project so far. Not even live yet.

Your comment gave me the idea to try scratch variables to get the $paginator.PageNumber value out from the pagination template.

That unfortunately only works for those theme templates that are called later in time. So for footer.html it works, but sidebar.html renders earlier than the pagination template, so the scratch variable is always empty there.

I have a very similar issue. I want to modify the pagination count on the HOME homepage, not the rest of the pages.

(Edit remove the rest of my comment because morning…coffee…something)

I guess this gives a hint that the code calling .RelPermalink do not reference the actual page.

That is odd, right?

I also checked with the following page variables, but those are also always the same on each paginated index page:

  • .Content
  • A custom param for the index page
  • .UniqueID is also the same on each page.

Thanks, good to know I’m not the only one we tries to implement this. I’ve made a feature request here: