0
votes

I want to create an archive for old blog posts on my jekyll site. Previously, my structure was serving the contents of _posts on my website homepage, index.html. After reading the collections documentation and a few tutorials online, I have added a collection folder _archive to my structure and a test file inside called test-file.markdown.

However, the url mysite.com/archive/test-file fully regenerates my main index.html, not the collection contents.


Structure:

_archive
    index.html
    test-file.markdown
_includes
    about.html
    head.html
    ... other stuff ...
_layouts
    default.html
_posts
    post1.markdown
    post2.markdown
    ... other stuff ...
css
img
js
_config.yaml
... other stuff ...

test-file.markdown

---
layout: default
title: test
---

_config.yml

# Site settings
title: test
email: [email protected]
url: http://www.test.com


# Color settings (hex-codes without the leading hash-tag)
color:
  primary: ffffff #80B3FF
  primary-rgb: "24,288,156" #"128,179,255"
  secondary: 2c3e50 #FD6E8A
  secondary-dark: 233140 #A2122F
  third: 979797

collections:
  archive:
    output: true
    permalink: /archive/:path/

# Build settings
markdown: kramdown
permalink: pretty

mysite.com/archive/index.html

---
---
{% for p in site.archive %}
  {{ p}}
  {{ p.title }}
{% endfor %}

This re-renders the main index.html, not the contents of test-file.markdown.


How can I properly render the contents of _archive at mysite.com/archive/?


EDIT: added --- to index.html

2

2 Answers

1
votes

Did you add the:

---
---

{% for p in site.archive %}
{{ p}}
{{ p.title }}
{% endfor %}

on the top of the index.html file? If it's missing it won't run any content within in that file through jekyll's templating engine.

0
votes

It's hard to tell what your problem really is without seeing the whole site. Provide a repo URL if possible.


If the contents of /index.html are appearing in the /_archive/test-file.markdown output, the post loop is probably in the default layout file, since both of the files share that layout. The solution here would be to move the relevant content into /index.html.


I believe that your /_archive/index.html is not being output. Move /_archive/index.html to /archive.html. Jekyll doesn't process pages inside of the _archive folder because it starts with an underscore.

You'd then have these files output with your current config:

  • /archive/index.html
  • /archive/test-file/index.html

In my opinion, you should keep posts as posts, whether they are archived or not. You would then keep the URLs when posts are archived rather than having to set 301s or losing them to the great void.

To do this, add front matter to your archived posts:

---
archived: true
---

And your current posts (you could use defaults to save repetition):

---
archived: false
---

Exclude the archived posts in your main post loop:

{% assign posts = site.posts | where: "archived", false %}

Exclude the current posts on your archive page:

{% assign archived_posts = site.posts | where: "archived", true %}