How do I Conditionally set Title Link Based on Front Matter

I would like to format “link posts” slightly differently than normal posts. For a normal post, on the list template, the title is a permalink to the actual posting. For a link post, I want the title displayed on the list template to be a link to the external site I am linking to, and I want a symbol next to the title that is the actual permalink.

In my front matter I have these two fields:

link: true
linkurl: https://someother.site

And in my template I have

    {{ if .Params.link }}
      <a href="{{ .Params.linkurl }}">{{ .Title }}</a>
      <a href="{{ .Permalink}}">&#9737;</a>
    {{ else }}
      <a href="{{ .Permalink }}">{{ .Title }}</a>
    {{ end }}

However, I never get the true side of the conditional, the list always shows the title as a link to the posting on my site. What am I missing or doing incorrectly?

Your code behaves as you would like, provided the context (the dot) is a page. For example, from layouts/_default/home.html, this works fine:

{{ range .Site.RegularPages }}
  {{ if .Params.link }}
    <a href="{{ .Params.linkurl }}">{{ .Title }}</a>
    <a href="{{ .Permalink}}">&#9737;</a>
  {{ else }}
    <a href="{{ .Permalink }}">{{ .Title }}</a>
  {{ end }}
{{ end }}

If you need additional assistance, please post a link to the public repository for your project.

See https://discourse.gohugo.io/t/requesting-help/9132.

Let us see your code

Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone on your repo, then run hugo server in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.

If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.

Here’s a link to the repository: GitHub - zanshin/hugo.
Here’s the entire layouts/index.html file in question.

{{ define "main" }}
<div>
{{ range $index, $value := ( where .Site.RegularPages "Type" "posts" | first 10 ) }}
{{ if (ne $index 0) }}<hr>{{ end }}
  <section>
    <h1><a href="{{ .RelPermalink }}">{{ .Title }}</a></h1>
    {{ partial "metadata.html" . }}
    {{ .Summary }}
    {{ if .Truncated }}
        <a href="{{ .RelPermalink }}"> ... [Read More ...]</a>
    {{ end }}
  </section>
{{ end }}
</div>
{{ end }}

I’m capturing the index from the range so I can display a horizontal rule in front of every post, except the first one. I wonder if that alters the context (the dot).

In the code you posted above, I don’t see a reference to .Params.linkurl.

Whoops. Copy paste error. Wrong template.

{{ define "main" }}
  {{ $prev := 3000}}
  {{ range .Pages.ByPublishDate.Reverse }}
    {{ if .Date }}
      {{ if gt $prev (.Date.Format "2006") }}
        {{ if ne $prev 3000 }}
          </ul>
        {{ end }}
        <h2>{{ .Date.Format "2006" }}</h2>
        <ul>
      {{ end }}
      <li style="list-style-type:none;">
        {{ .Date.Format "Jan 02" }} »
        {{ if .Params.link }}
          <a href="{{ .Params.linkurl }}">{{ .Title }}</a>
          <a href="{{ .Permalink}}">&#9737;</a>
        {{ else }}
          <a class="strong" href="{{ .Permalink }}">{{ .Title }}</a>
        {{ end }}
      </li>
      {{ $prev = .Date.Format "2006"}}
    {{ end }}
  {{ end }}
{{ end }}

When I visit http://localhost:1313/posts/ I see this:

It is generated by layouts/_default/list.html.

Looks right to me…

1 Like

I’m an idiot. I have been looking at layout/_default/list.html instead of layout/index.html. Adding the conditional logic to the correct html templates results in what I was after. Thanks for you help and patience.

2 Likes

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