Blog | Tristan Kernan

“That some of us should venture to embark on a synthesis of facts and theories, albeit with second-hand and incomplete knowledge of some of them – and at the risk of making fools of ourselves” (Erwin Schrödinger)

Hide Category From Index in Pelican

Fitting enough for my first TIL post, I had to figure out how exclude posts in the TIL category from my pelican blog, without breaking pagination. The feature has been requested in the pelican github, and thankfully the code snippet at the end has the solution:

class ExcludeWriter(Writer):

    def write_file(
        self,
        name,
        template,
        context,
        relative_urls=False,
        paginated=None,
        template_name=None,
        override_output=False,
        url=None,
        **kwargs
    ):
        articles = kwargs.get("articles")
        page_name = kwargs.get("page_name")
        if page_name == 'index':
            ### set categories to exclude here
            articles = [a for a in articles if not a.category == 'TIL']
            kwargs["articles"] = articles

        super().write_file(
            name=name,
            template=template,
            context=context,
            relative_urls=relative_urls,
            paginated=paginated,
            template_name=template_name,
            override_output=override_output,
            url=url,
            **kwargs
        )


def get_writer(sender):
    return ExcludeWriter


def register():
    """Register the new plugin"""
    signals.get_writer.connect(get_writer)

register()  # <-- this line added to the code in the snippet

Place this in pelicanconf.py and restart the devserver.