Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IsMenuCurrent() returns false if menu item defined inside config file #3459

Closed
lebarde opened this issue May 12, 2017 · 13 comments
Closed

IsMenuCurrent() returns false if menu item defined inside config file #3459

lebarde opened this issue May 12, 2017 · 13 comments

Comments

@lebarde
Copy link
Contributor

lebarde commented May 12, 2017

It appears that IsMenuCurrent or HasMenuCurrent do not return true everytime it should.

  • It seems to work for parent/children.
  • It seems to always work on homepage.
  • It seems to work on menu items defined inside the page/post's frontmatter.

But it seems not to work on menu items defined inside the main configuration file.

Is this a confirmed bug? At least this is the case for me.

Reference of discussions this is based upon:

@bep
Copy link
Member

bep commented May 12, 2017

Is this a confirmed bug? At least this is the case for me.

No, but as it depends on the URL you set in config to do the matching, it may be hard to get right. Unless you provide a complete and failing example, there is nothing to do here.

@lebarde
Copy link
Contributor Author

lebarde commented May 13, 2017

I just uploaded a working site where menus do not work: https://github.com/lebarde/apo
I generated the content using hugo --buildDrafts.

As you can see here, the home button is active on the homepage. But on the biographie/ page the biographie menu link is not marked as active.

Note that this is the case if I create the menu in the pages' front matters.

@lebarde
Copy link
Contributor Author

lebarde commented May 13, 2017

One nice resolution would be the ability to define menu items by their identifiers or their routes (I assume that identifiers is easier to implement). In that way you do

menu:
  main: 
    - Name: Home
      Title: My home title
      URL: /
      Weight: 10
    - Name: "Biographie"
      Identifier: "biographie"
      Weight: 20

In this case, Hugo would look for a page which identifier is named biographie.

IMO the page file has to define its identifier (or it is defined automatically from filename or main title, I don't know the current policy on that), and then you define the menu globally. This is for me a good way to have an overview of the whole menu.

@stale
Copy link

stale bot commented Dec 6, 2017

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

@stale stale bot added the Stale label Dec 6, 2017
@stale stale bot closed this as completed Dec 27, 2017
@Brixy
Copy link

Brixy commented Mar 5, 2018

Hi all!

I keep experiencing the same problem. Don’t know whether it is in issue or a problem of understanding.

Unless you provide a complete and failing example, there is nothing to do here.

Attached you find an example. “Home“ and “blog” are provided with a cur class (-> bold text) when active; contact is not.

Thanks!

zipped.zip

@cfjedimaster
Copy link

I am seeing the same issue. While my code isn't the exact same as @Brixy - I have a similar setup. Menu is defined in the config file - renders out just fine but IsMenuCurrent fails to work.

@skylee91
Copy link

skylee91 commented Aug 4, 2018

Yes, I have the same issue as @cfjedimaster too. Anyone has any idea to fix it?

@fabianlaule
Copy link

I'm facing the same issue with Hugo 0.47.1

@kalilsn
Copy link

kalilsn commented Aug 29, 2018

Solved this for myself just now by adding trailing slashes to my menu items in the config! I didn't realize it was matching by URL, but using url = "/issues/" instead of url = "/issues" did the trick.

@Brixy
Copy link

Brixy commented Aug 30, 2018

Hi @kalilsn

The config file in the zip file linked above uses trailing slashed for url. Yet, the class .cur is not assigned to the last menu item, “contact”, when active.

Have you success when you run hugo with that test folder?

@sysoce
Copy link

sysoce commented Dec 17, 2018

When defining multilingual menus the IsMenuCurrent function does not work as expected.
The code taken straight from the docs can confirm this bug; https://gohugo.io/content-management/multilingual/#menus

Expected result: Both menus get the "active" class when navigating to the respective page.
Result: Only the defaultContentLanguage menu gets the active class.

Temporary fix: set the "ja" menus URL to url = "/ja/"

config.toml:

defaultContentLanguage = "en"

[languages.en]
weight = 0
languageName = "English"

[[languages.en.menu.main]]
identifier = "home"
url    = "/"
title   = "Home"
weight = 0

[languages.ja]
weight = 10
languageName = "日本語"

[[languages.ja.menu.main]]
identifier = "home"
url    = "/"
title   = "ホーム"
weight = 0

menu partial:

<ul>
    {{ $currentPage := . }}
    {{ range .Site.Menus.main }}
    <li>
        <a href="{{ .URL | absLangURL }}" class="{{ if $currentPage.IsMenuCurrent "main" . }}active{{ end }}">
        	{{ .Name }}
        </a>
    </li>
    {{- end }}
</ul>

@moorereason
Copy link
Contributor

Please don't comment on old, closed issues. Use the discussion forums or open a new issue.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants