Multilevelmenu without drafts from config.toml

Hi,
I have some problems to render my navigation without drafts. Is there an easy example snippet somewhere to understand why drafts are always rendered?

Or whats wrong with my code?

<ul class="nav-vertical first-level">
	{{ $currentPage := . }}
	{{ range .Site.Menus.main }}
		{{ if .HasChildren }}
			<li class="{{ if $currentPage.HasMenuCurrent "main" . }}active{{ end }}">
				<a href="{{ .URL }}">{{ .Name }}</a>
				<ul class="second-level">
				{{ range .Children }}
					<li class="{{ if $currentPage.HasMenuCurrent "main" . }}active{{ end }}">
						// here the link
					</li>
				{{ end }}
				</ul>
			</li>
		{{ else }}
			<li class="{{ if $currentPage.HasMenuCurrent "main" . }}active{{ end }}">
				// here the link
			</li>
		{{ end  }}
	{{ end }}
</ul>

Hi,

How are you running Hugo? Running hugo or hugo server with the -D or --buildDrafts flag will include draft content.

1 Like

I tested it with and without -D - I do it that way:

hugo server --disableFastRender

Hi,

In that case, we probably need to see more of your code to help you troubleshoot. Do you have your code somewhere we can look at?

Thank you, I uploaded it on my git server for you: https://git.georgkaser.com/georgkaser/my-hugo-project/tree/master so you can browse the code…

You added those items to the main menu from your config.toml. Since those menu items aren’t from pages, they aren’t affected by the range, hence the confusion.… sorry, I mistyped there.

When you are range over the main menu, it gets all the menu items. Then, you start filtering them by conditionals, but the items you set in your config aren’t included, hence the reason they show up at all times.

You’ll have to re-think how you build that menu. :slight_smile:

So, i have to generate the menu from the pages instead of the config.toml?

I’m not sure what you are trying to do. Aside from the draft status of the items in question, the menu looks good. The original question was about draft status, and we know that isn’t the issue, so could you clarify what you are trying to do?

Thank you for your help - I thought that the draft status is considered when I create the menu from config.toml - this is not the case, isn’t it?
I read now the docs and I think I should try to create the menu from front matter.

It is not the case. And that is useful, because you can add any links to the menu from config. You could put your email as a menu item, or link to other sites you run. That sort of thing.

For draft status, yeah, go with front matter. :slight_smile:

I understand, thanks for your help!

Hmm, it’s not that easy. I think i didn’t understand some concepts of rendering menus and managing content. I created a new theme for testing purposes - just for the menu. And the output was not what I expected.

Look at that screenshot - why is it not possible to render the content with subcontent as unordered list??

That’s not how we do it here: share your code. Presume I can’t see your screenshot.