I’m having a hard time wrapping my around the explanation given for using this snippet
<ul>
{{ range .Site.Menus.main }}
<li><a href="{{ or .URL .PageRef }}">{{ .Name }}</a></li>
{{ end }}
</ul>
why does the standard
<ul>
{{ range .Site.Menus.main }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ end }}
</ul>
not work even though the page exists and can be accessed by typing the URL directly.
Thanks in advance for any clarification
I had a similar issue with the template rendering a blank href attribute for just one of the links in production while everything worked fine locally, fixed it by changing the baseUrl configuration variable.
From https://gohugo.io/methods/menu-entry/pageref:
The use case for this method is rare.
In almost also scenarios you should use the URL
method instead.
To give you an idea of rarity, I have seen only one scenario where the PageRef
method was needed.
If you need help with one or menu entries not rendering as expected, you will need to share some information with us.
See Requesting Help.
Let us see your code
Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone on your repo, then run hugo server in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.
If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.
1 Like
Thank you for the prompt response. I’ll make sure to keep the scenario you mentioned in mind when implementing more complex menus.
I already fixed the issue with the menu entry not rendering properly by modifying the baseurl. Thanks again
Whatever you did may have solved the problem, but the way you did it is wrong. Maybe you specified the url
property when defining your menu entries in your site configuration, when you should have specified the pageRef
property. Don’t confuse the pageRef
property with the PageRef
method.
https://gohugo.io/content-management/menus/#properties-site-configuration
I didn’t mix them up, I used the pageRef
property for all internal links.
I should add that the issue only occurred in production and not locally, hence the baseurl fix.
So your menu entries were correct, but the baseURL did not reflect the URL of your production site. Is that correct?