Ranging posts by date in .md

I’ve been able to range through my posts by date on my index.html page. However, when I copy this code into another page (recent.md), it now doesn’t work. I made sure to put a .html shortcode so it would allow my markdown file to display html, but this didn’t help.

recent.md:

---
title: "Post 1"
publishDate: "1 Jan 2006"
---

{{< html >}}
<ul>
{{ range .Pages.ByPublishDate }}
<li>
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
<time>{{ .Date.Format "2 Jan 2006" }}</time>
</li>
{{ end }}
</ul>
{{< /html >}}

What appears on website: (bad)
image

What the same code appears as on my index.html: (good)
image

Repo: GitHub - jpnfighter/steve-site: hugo website

Template code goes in template files, not in markdown.

I am not sure how to go about this.
I have renamed recent.md to recent.html and moved it into themes\steve-theme\layouts_default

You have a section for posts. Each item in the section should be a post. Why do you want to include a summary of the posts as an item within the posts section? That doesn’t make sense.

I was wanting to have on my index.html a section for the top 3 most recent posts. Alongside that, I wanted a link to direct to a page where it displays all the most recent posts, so every post sorted by date, not just the top 3 most recent posts.

Which index.html? Your home page? The list page for posts? Something else?

I’ve been meaning my homepage when I say index.html. My bad

In your home page template:

{{ partial "recent.html" (dict "section" "posts" "qty" 3) }}

layouts/partials/recent.html (something like…)

{{ with where site.RegularPages "Section" .section | first .qty }}
  <p>Recent {{ $.section }}:</p>
  <ul>
    {{ range .ByDate }}
      <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
    {{ end }}
  </ul>
{{ end }}
1 Like

To show the most recent at the top of the list, change this:

{{ range .ByDate }}

to this:

{{ range .ByDate.Reverse }}
1 Like