Jekyll Github pages how to hide a post
Asked Answered
A

5

28

I am using jekyll with Github pages for my website. I am trying to make some posts not visible in the home but they can be linked from another post. In the frontmatter I tryed to add a field visible like this:

---
layout: post
title: 
excerpt: 
visible:1
---

And then in the index.html file I did a if check:

<div class="posts">
  {% for post in paginator.posts %}
  {% if post.visible== 1  %}

  <div class="post">
    <h1>
      <a href="{{ post.url }}">
        {{ post.title }}
      </a>
    </h1>

    <span class="post-date">{{ post.date | date_to_string }}</span>
        <a class="subtitle" href="{{ post.url }}">
           {{ post.excerpt }}
        </a>
      </a>
  </div>
  {% endif %}
  {% endfor %}
</div>

The idea is that when I set 0 in the visible field, the post won't be visible in the home. Unfortanely this is not working, do you have any hints? Thanks

Antiperspirant answered 29/5, 2014 at 13:53 Comment(1)
As shackett points out below, you can hide posts from the homepage and pagination by adding hidden: true to your YAML frontmatter. The posts will still be accessible, they just won't show up on your homepage.Pronounce
S
7

Try to change your front-matter from visible:1 to visible: 1.

I just tried to reproduce your example on my machine, and I found that Jekyll seems to picky about the blanks in the front-matter.

With visible: 1, your example works for me.

With visible:1, Jekyll outputs the following error message while building the site:

YAML Exception reading C:/foo/bar.md: (): could not find expected ':' while scanning a simple key at line 5 column 1

...but it still finishes building and the generated site works, except that the post is not visible.

Society answered 29/5, 2014 at 18:0 Comment(1)
It's not Jekyll being picky but rather a specificity of YAML, see en.wikipedia.org/wiki/YAML#Associative_arrays ("Keys are separated from values by a colon+space.")Confessor
C
50

This works for me:

---
layout: post
title: About Lumen
published: false
---

See [About]({{ site.baseurl }}/about)
Cope answered 6/1, 2015 at 13:29 Comment(3)
Setting published to false will not only hide the post but also make it unavailable (i.e. it is exclude from the site)Confessor
No what the OP asked for, but I found it useful.Hans
@Confessor Apparently you can preview the post locally by running jekyll server --unpublishedSteerage
F
15

If you want to exclude a post/page from pagination you can add hidden: true to the YAML frontmatter. https://github.com/jekyll/jekyll-paginate/issues/6

Florence answered 26/9, 2016 at 1:51 Comment(2)
This feature is great since it allows you to hide the post from the page except for ones who have the link. Note: Technical guys could easily take it out from your repo if you use Github Pages so you shouldn't hide something you don't want others to see.Agram
Note: if like me you're using custom code to show posts e.g. per category, this won't work. But if you do use custom code, you can probably check this flag, or any custom flag, as needed (as in OP's code).Inharmonic
S
7

Try to change your front-matter from visible:1 to visible: 1.

I just tried to reproduce your example on my machine, and I found that Jekyll seems to picky about the blanks in the front-matter.

With visible: 1, your example works for me.

With visible:1, Jekyll outputs the following error message while building the site:

YAML Exception reading C:/foo/bar.md: (): could not find expected ':' while scanning a simple key at line 5 column 1

...but it still finishes building and the generated site works, except that the post is not visible.

Society answered 29/5, 2014 at 18:0 Comment(1)
It's not Jekyll being picky but rather a specificity of YAML, see en.wikipedia.org/wiki/YAML#Associative_arrays ("Keys are separated from values by a colon+space.")Confessor
M
0

You need to modify the _layout/home.html file (In your case, it might be the index.html file).

Try to use an if-endif statement,like this:

{%- for post in site.posts -%}
  {% if post.hide == null or post.hide == false %}
    <li>
    {%- assign date_format = site.minima.date_format | default: "%b %-d, %Y" -%}
    <span class="post-meta">{{ post.date | date: date_format }}</span>
    <h3>
     <a class="post-link" href="{{ post.url | relative_url }}">
      {{ post.title | escape }}
     </a>
     </h3>          
    </li>
  
  {% endif %}
{%- endfor -%}

Then, hiding a post by hide: true. For example:

published: true
title: Some title
layout: post
hide: true
May answered 3/11, 2021 at 11:58 Comment(0)
F
0

The other answers didn't work for me, unless I edited the HTML files, which I wanted to avoid. Something that worked for me was simply adding _ to the name of the markdown file.

For instance, I had a post with the following title: 2023-05-02-yoga.md

I renamed it to: _2023-05-02-yoga.md

And the post didn't show anymore.

Fasces answered 9/2 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.