Build a permalink of most recent post for the main menu

Hey, I’m having a hard time finding exactly how to do this. I want to add a URL to my main menu of the most recent post from either my “journal” with the category of “now”. Or have a separate content directory of /now and add the most recent post’s permalink to the main menu.

[[main.menu]]
  identifiers = "now"
  name = "Now"
  url = "/now/{{ print the permalink to the most recent post in this directory }}"
  weight = 2

I tried something like this a few different ways with no luck /now/{{ (index .Site.RegularPages 0).Permalink }}

I’d love an example for both the category option or separate content option.

Thanks!

To render a menu item that points to the latest of pages in the “journal” section to which the category “now” has been assigned…

[[menus.main]]
name = 'Home'
pageRef = '/'
weight = 10

[[menus.main]]
name = 'Journal'
pageRef = '/journal'
weight = 20

[[menus.main]]
identifier = 'most-recent-journal-entry-with-category-now'
name = 'Now'
weight = 30

Then render your menu with something like:

{{ $p := where site.RegularPages "Section" "journal" }}
{{ $p = where $p "Params.categories" "intersect" (slice "now") }}
{{ $p = index $p.ByLastmod.Reverse 0 }}

{{ with site.Menus.main }}
  <nav class="menu">
    {{ range . }}
      {{ $url := .URL }}
      {{ if eq .Identifier "most-recent-journal-entry-with-category-now" }}
        {{ $url = $p.RelPermalink }}
      {{ end }}
      {{ if $.IsMenuCurrent .Menu . }}
        <a class="active" aria-current="page" href="{{ $url }}">{{ .Name }}</a>
      {{ else }}
        <a href="{{ $url }}">{{ .Name }}</a>
      {{ end }}
    {{ end }}
  </nav>
{{ end }}
1 Like

Thank you so much! I had to edit a few things on my end, but I got it working. I appreciate it :+1:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.