How to Emulate Linked List Behavior

#1

Hello,

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>
        {{end}}
        {{if eq .Type "link"}}
          <h3 class="post-title"><a href="{{ $.Params.linkurl }}">{{ .Title }}</a></h3>
        {{end}}

Thank you!

#2

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

1 Like
#3

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 }}
    </div>
  {{ 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?

#4

You can reference the front matter param like so:

{{ .Params.nameOfParamHere }}
#5

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?

#6

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

#7

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.

#8

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

href="{{ .Permalink }}"

Do this:

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

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

<a href="{{ .Params.link | 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