How to Emulate Linked List Behavior


I’m currently building a site that will be have a linked list post type.

  • I specify the post type with type: "link" in a post’s front matter.
  • I have a layout for this like post type located in /layouts/link/single.html.
  • In each link post, I have a param linkurl that specifies an external link to link to. Is there a way to call this param in my index.html and insert it into the title of the post as the href destination?

This is what I have now, but of course it doesn’t work on index.html.

{{if eq .Type "post"}}
          <h1 class="post-title"><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
        {{if eq .Type "link"}}
          <h3 class="post-title"><a href="{{ $.Params.linkurl }}">{{ .Title }}</a></h3>

Thank you!

You need to use the range function to render posts on the homepage. The forum and the Docs have lots of examples.

1 Like

Hi, thanks for your reply. I am already using a range function to render posts on the homepage. I’m just having trouble rendering them the same way as single posts appear. For example, I’d like my post title to link to an external URL. This works fine on my single page, but I can’t figure out how to do that on the homepage. All the examples sites I’ve looked at display posts on the home page like below.

{{ range .Paginator.Pages }}
    <div class="post">
        <h1 class="post-title"><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
        <span class="post-date">{{ .Site.Params.DateForm | default "Jan 2, 2006" | .Date.Format }}</span>
        {{ .Content }}
  {{ end }}

This displays my normal posts just fine, but doesn’t work for my link posts because of the {{ .Permalink }} parameter. For links posts, I want to somehow use a link parameter I have specified in the front matter. Is there a way to make that happen on the homepage?

You can reference the front matter param like so:

{{ .Params.nameOfParamHere }}

Yes, I can use it for the single posts. I’m having trouble pulling single post parameters on my home page. Is there anyway to do that?

I’m not sure what you mean. Can you provide an example?

Yes, no problem.

I want to have two content types on my blog - post and link.

The post type is the usual blog post, nothing special.

The link type is meant to emulate a linkblog, where clicking the title will redirect to an external URL, which is specified in the frontmatter with a link param. This is all working fine on a linkblog posts’s single.html display.

What I’m having trouble with is figuring out how to get the external hyperlinked title to appear on the front page. Right now, I’m using this for my frontpage, which uses the post’s permalink as the destination for the title link.

{{ range .Paginator.Pages }} &lt;div class="post"&gt; &lt;h1 class="post-title"&gt;&lt;a href="{{ .Permalink }}"&gt;{{ .Title }}&lt;/a&gt;&lt;/h1&gt; &lt;span class="post-date"&gt;{{ .Site.Params.DateForm | default "Jan 2, 2006" | .Date.Format }}&lt;/span&gt; {{ .Content }} &lt;/div&gt; {{ end }}

What I’m trying to do is just display all posts without having to specify where the title link should go… in theory just keeping the same settings as single.html format.

I see. So if I’m understanding you right, then instead of this:

href="{{ .Permalink }}"

Do this:

href="{{ .Params.someLink }}"

You already know about default, so use it for your title link:

<a href="{{ | default .Permalink }}">{{ .Title }}</a>

In that instance, link is the name of my front matter variable. If yours is linkurl, use that instead.

And if you want any more assistance, please read Requesting Help. There is only so much we can do without a repo to look at and reproduce the issue you are having.

1 Like