Files
hugo/content/en/methods/menu-entry/PageRef.md
T
Bjørn Erik Pedersen e477373487 Squashed 'docs/' changes from 227aab619..8390a4a3a
8390a4a3a Add Void Linux installation instructions
d6099aae8 Update link to PostCSS plugins
25dad4693 netlify: Hugo 0.139.4
2b1fa118c Fix typo
3ef1eb505 Update hosting-on-aws-amplify
c0f6d35d6 Fix typo
aa54d4301 Correct directory name
98aa26bdb Improve instructions for hosting with AWS Amplify
a07638a80 Add new-in badges
6ad018055 netlify: Hugo 0.139.3
1050835d6 Update title of hugo.Store page
ebbd2e851 Clarify the shortcode Ordinal method
b7716ed95 Revise code block render hook for Mermaid diagrams
f1da9b6ea netlify: Hugo 0.139.2
d8ac9f428 Downgrade the Go toolchain in go.mod to a slightly older Go version
254b3c4f2 netlify: Hugo 0.139.1
03e666038 Add hugo.Store, site.Store and Shortcode.Store
157e8983b Update Anchorize.md
59fa9f214 Document the PageRef menu entry method
bda544cce docs(transform.Unmarshal): match lang attribute to title language in examples
1985886bd Adjust front matter of shared Markdown snippets
da5bd70d1 Fix typo
431b65d6b Update theme
b63ef69f5 Update style guidance
d50ed3422 Remove old new-in badges
12bfb9933 Update docs.yaml
0b936cacd netlify: Hugo 0.139.0
ab7668b4d dartsass: Add silenceDeprecations option
154df9bfc Merge commit '838bd312b1a287bb33962ad478dbc54737654f35'
efa80477c docs: Regen CLI docs
ad99e4a7a docs: Regenerate CLI docs

git-subtree-dir: docs
git-subtree-split: 8390a4a3ac
2024-12-11 09:53:33 +01:00

2.9 KiB

title, description, categories, keywords, action, toc
title description categories keywords action toc
PageRef Returns the `pageRef` property of the given menu entry.
related returnType signatures
/methods/menu-entry/URL
string
MENUENTRY.PageRef
true

The use case for this method is rare.

In almost also scenarios you should use the URL method instead.

Explanation

If you specify a pageRef property when defining a menu entry in your site configuration, Hugo looks for a matching page when rendering the entry.

If a matching page is found:

  • The URL method returns the page's relative permalink
  • The Page method returns the corresponding Page object
  • The HasMenuCurrent and IsMenuCurrent methods on a Page object return the expected values

If a matching page is not found:

  • The URL method returns the entry's url property if set, else an empty string
  • The Page method returns nil
  • The HasMenuCurrent and IsMenuCurrent methods on a Page object return false

{{% note %}} In almost also scenarios you should use the URL method instead.

{{% /note %}}

Example

This example is contrived.

{{% note %}} In almost also scenarios you should use the URL method instead.

{{% /note %}}

Consider this content structure:

content/
├── products.md
└── _index.md

And this menu definition:

{{< code-toggle file=hugo >}} menus.main name = 'Products' pageRef = '/products' weight = 10 menus.main name = 'Services' pageRef = '/services' weight = 20 {{< /code-toggle >}}

With this template code:

{{< code file=layouts/partials/menu.html >}}

{{< /code >}}

Hugo render this HTML:

<ul>
  <li><a href="/products/">Products</a></li>
  <li><a href="">Services</a></li>
</ul>

In the above note that the href attribute of the second anchor element is blank because Hugo was unable to find the "services" page.

With this template code:

{{< code file=layouts/partials/menu.html >}}

{{< /code >}}

Hugo renders this HTML:

<ul>
  <li><a href="/products/">Products</a></li>
  <li><a href="/services">Services</a></li>
</ul>

In the above note that Hugo populates the href attribute of the second anchor element with the pageRef property as defined in the site configuration because the template code falls back to the PageRef method.