特定URL隐藏分类文章

Posted by Eric on July 18, 2025

通过屏蔽关键词的方式伪隐藏文章,并通过特定URL查找到这些文章

原理分析

目的: 要实现通过特定URL访问隐藏的分类文章,同时不让这些文章出现在首页或其他公开位置

方法分析:

  • 如果只是不想让文章出现在首页 → 方法1(hidden: true)
  • 如果希望完全隐藏,除非知道 URL → 方法2(_drafts)
  • 如果需要更严格的访问控制 → 方法3(私有分支)

本文中只介绍方法1

举例说明: 比如想要将work分类下的文章隐藏起来,输入https://<your-site>/work才可以看到

具体步骤

1. 在文章的 Front Matter 中添加 hidden: true 和 category: work

  • 隐藏文章设置,隐藏属性为,分类为work
1
2
hidden: true  # 不在首页显示
category: work  # 属于 work 分类
  • 正常文章设置,隐藏属性为
1
hidden: false  # 在首页显示

2. 在_config.yml添加内容确保首页不显示 hidden 文章

  • 在首页只显示hidden属性为false的文章
1
2
3
4
5
6
defaults:
  - scope:
      path: ""
      type: posts
    values:
      hidden: false

3. 创建 /work/index.html 页面

  • 该步骤目的是在https://<your-site>/work只显示work分类的文章
  • 将根目录的index.html直接复制到新建目录work下,在此基础上做一些修改
    1. Front Matter中加入permalink: /work/,以设置访问路径是https://<your-site>/work
    2. for post in site.posts中加入 `收尾,以确保只显示work分类的文章
  • 以我的index.html为例
    • 有些个人博客(比如我)搭建时用了分页插件(比如jekyll-paginate),所以首页的 index.html 里用的是 paginator.posts,而不是 site.posts。但分页插件 paginator只在首页生效,所以/work页面还是需要site.posts,只需都替换成site.posts即可
  • 修改后我的work/index.html如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
---
layout: page
description: "「work」"
permalink: /work/  # 设置访问路径
---

{% for post in site.posts %}
  {% if post.category == "work" %} # 如果为work分类的文章
    <div class="post-preview">
        <a href="{{ post.url | prepend: site.baseurl }}">
            <h2 class="post-title">
                {{ post.title }}
            </h2>
            {% if post.subtitle %}
            <h3 class="post-subtitle">
                {{ post.subtitle }}
            </h3>
            {% endif %}
            <div class="post-content-preview">
                {% if post.lang == 'en' %}
                    {{ post.content | strip_html | truncate:300 }}
                {% else %}
                    {{ post.content | strip_html | truncate:200 }}
                {% endif %}
            </div>
        </a>
        <p class="post-meta">
            Posted by {% if post.author %}{{ post.author }}{% else %}{{ site.title }}{% endif %} on {{ post.date | date: "%B %-d, %Y" }}
        </p>
    </div>
  {% endif %} # end for 确保如果为work分类的文章
<hr>
{% endfor %}

—————————————————————————————————————

The end

—————————————————————————————————————

首页