Range through .Title without leading "The"

Is it possible to range through pages by title while omitting any leading articles like The, An, or A? For example, if I have three pages named “Something Great”, “Another Post”, and “The Beginning”, sorting by page title would yield:

  • Another Post
  • The Beginning
  • Something Great

Currently, Hugo will put “The Beginning” after “Something Great”. I’d like to be able to drop those small leading words that typically don’t get alphabetized.

We don’t have a built-in sorting implementation for that. You’d need to range through the pages and create a dictionary with your trimmed titles as the key.

Thank you for the clarification.

Do you happen to have a link that goes into the range function? I have to admit, I can’t quite wrap my head around using more than a basic {{ range .Pages }} ... {{ end }}, so using keys and values in my range is beyond my current understanding.

Okay, here’s a hacky solution that appears to be working for me.


{{ $sorted := dict }}
{{ range . }}
    {{ $title := strings.TrimPrefix "An " .Title }}
    {{ $title = strings.TrimPrefix "A " $title }}
    {{ $title = strings.TrimPrefix "The " $title }}
    {{ $to_merge := dict $title . }}
    {{ $sorted = merge $sorted $to_merge }}
{{ end }}
{{ $sorted = sort $sorted }}
{{ return $sorted }}


{{ range partial "sort-title" .Pages }}
{{ end }}

The trimming isn’t very intelligent. It’s case sensitive, and there’s a possibility (however remote) that some titles could be overly trimmed. However, it seems to get the job done.

1 Like

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