Hide posts with a category of name x

So I have this wacky idea - someone may tell me a better way. Please do!

I am wanting to dump a new “thing” (usually a weblink and some text) as a micro blog item as I come across them… Then once a month subscribers get my linked-list of things. Makes sense?

I dont want this list of “things” being seen in my micro blog. It should be maybe on a seperate page - or maybe even - nowhere.

So do I have to do my own theming to do this is or is there a better way?

Will

This would have to be an edit to your existing theme currently. It’s a little tricky to do, but a pretty minor adjustment just to the part that says “get all the posts and paginate them” to exclude a category.

Thanks @jsonbecker - I thought that may be the case

I think another way I could achieve the same thing (maybe better idea) is to use the bookmarks feature and categorise all these “things” with the correct tag… I dont think there is a natty newsletter option to make an email from these tags but thats ok… any better ideas welcome!

Hello,

I have a question that seems directly relevant to the question that was asked by the OP several months ago. If this should be moved to its own distinct post, please let me know.

I am trying to exclude all posts with the category hide from my blog’s homepage. I am not trying to exclude it from my RSS feed or the timeline. I just want to hide it on my blog. I have read other help posts and my general understanding is that it should be possible to exclude a post in this way. I have duplicated a Micro.blog theme (Sumo) so that I can make edits. And my understanding is that I may need to edit one of these two files (maybe others?) to make this happen:

layouts/_default/list.html: Used for section and taxonomy pages (collections of content).
layouts/index.html: Used exclusively for the homepage.

Before I go any further, I would appreciate any guidance that anyone might be able to provide about which file(s) in my custom template should be edited. I really want to achieve this capability. But my knowledge of Hugo is limited. I’m trying to learn!

Thank you,

Will

Sumo has a feature its author invented called Microhooks. I’d actually use this Microhooks

But you also could simply use the layouts/index.html page. The only thing is /categories/hide will still exist on the web and you may be able to see the hidden posts on your archive page. I’m not 100% clear on that. I think the micro hook will remove it all places both methods are feasible.

If you search there are many topics where people ask about hiding categories.

Thank you @jsonbecker! I appreciate your reply. You got me to go back and review some of the of the posts on this topic that I’d saved. In one of them, you described excluding certain categories from my homepage and archive. And my understanding is that I would need to edit these files in my custom theme to do that.

layouts/index.html
layouts/_default/list.archivehtml.html

But I can’t seem to find an example of having successfully done this with either Sumo or Tiny themes.

I appreciate your suggestion to explore microhooks, which seem promising. But I’m having more success slowly building my understanding of how the Hugo code works, which is not saying much I guess.

I am 98% sure @pratik does this successfully so he may have some code examples for you. This is also one where chatgpt/copilot works really well.

Yes, I did see @pratik’s example (helpfully written up at the Tiny Theme Tutorial page) and have tried to follow it but I’m a few steps behind in understanding how to do this. It also seems to involve microhooks which I’m still wrapping my head around.

I confess, that I have been making liberal use of ChatGPT, in my efforts to understand what’s going on. None of these have worked when I’ve attempted to make changes to layouts/index.html that would exclude posts based upon category yet. But using it to explain the code, line-by-line, has been helpful to a novice like me.

You don’t have to use microhooks if you want to only exclude posts from a specific category on your homepage. You will have to categorize the “things” posts with a specific category though. Simply plop these three lines at the top of your homepage code:

    {{ $posts := where .Site.RegularPages "Type" "post" }}
    {{ $hidden_posts := where $posts "Params.categories" "intersect" (slice "Linking Park" "Now") }}
    {{ $filtered_posts := $posts | symdiff $hidden_posts }}

Replace “Linking Park” and “Now” with your category or categories that you want to exclude. Posts with these will still be on blog but not on your homepage. It can be on its own category page.

For the newsletter, if you want to send only posts with these “things”, you can set it up in the Newsletter settings on Micro.blog.

Thank you @pratik. I just tried placing the three lines you provided at the top of my layouts/index.html page, changing the two categories you provide for a single category hide.

It looked like this:

    {{ $posts := where .Site.RegularPages "Type" "post" }}
    {{ $hidden_posts := where $posts "Params.categories" "intersect" (slice "hide") }}
    {{ $filtered_posts := $posts | symdiff $hidden_posts }}

{{ define "main" }}
{{ if templates.Exists "partials/microhook-post-list.html" }}
{{ partial "microhook-post-list.html" . }}
{{ else }}

{{ if templates.Exists "partials/microhook-before-post-list.html" }}
{{ partial "microhook-before-post-list.html" . }}
{{ end }}
<div class="posts h-feed">
    <div class="post_list" role="main">

        {{ $paginator := .Paginate (where .Site.Pages.ByDate.Reverse "Type" "post") }}

        {{ range .Paginator.Pages }}
        <div class="post-preview h-entry {{ range .Params.categories }} {{ . | urlize | lower }}{{ end }}">
            {{ if templates.Exists "partials/microhook-post-list-byline.html" }}
            {{ partial "microhook-post-list-byline.html" . }}
            {{ else }}
            <div class="post-date-wrapper">
            <a href="{{ .Permalink }}" class="post-date u-url"><time class="dt-published" datetime="{{ .Date.Format "2006-01-02 15:04:05 -0700" }}">{{ .Date.Format "January 2, 2006" }}</time></a>
            </div>
            {{ end }}
            {{ if .Title }}
                <h2 class="post-title p-name"><a href="{{ .Permalink }}">{{ .Title }}</a></h2>
                    {{ if in .RawContent "<!--more-->" }}
                        <div class="p-summary">
                            {{ $splitContents := split .RawContent "<!--more-->" }}
                            {{ $summary := index $splitContents 0 }}
                            {{ $summary := replaceRE "\\[\\^.*?\\]" "" $summary }}
                            {{ $summary := replaceRE "\\n\\[\\^.*?\\]:.*" "" $summary }}
                            <p>{{ $summary | markdownify }}</p>
                            <p><a class="read-more" href="{{ .Permalink }}"><button>
                                {{ if templates.Exists "partials/microhook-read-more-text.html" }}
                                {{ partial "microhook-read-more-text.html" . }}
                                {{ else }}
                                Read More →
                                {{ end }}
                            </button></a></p>
                        </div>
                    {{ else }}
                        <div class="e-content with-title">
                            {{ .Content }}
                        </div>
                    {{ end }}
            {{ else }}
                <div class="e-content without-title">
                    {{ .Content }}
                </div>
            {{ end }}
            {{ if templates.Exists "partials/microhook-below-post-in-list.html" }}
            {{ partial "microhook-below-post-in-list.html" . }}
            {{ end }}
        </div>
        <hr class="article-break">
        {{ end }}
    </div>
</div>
{{ if templates.Exists "partials/microhook-after-post-list.html" }}
{{ partial "microhook-after-post-list.html" . }}
{{ end }}
{{ if templates.Exists "partials/microhook-pagination.html" }}
{{ partial "microhook-pagination.html" . }}
{{ else }}
<div class="post-nav">
    {{ if $paginator.HasPrev }}
    <span class="prev">
        <a href="{{ $paginator.Prev.URL }}" title="Previous Page"><button>← Newer</button></a>
    </span>
    {{ end }}
    {{ if $paginator.HasNext }}
    <span class="next">
        <a href="{{ $paginator.Next.URL }}"><button>Older →</button></a>
    </span>
    {{ end }}
</div>
{{ end }}
{{ end }}
{{ end }}

However, this was not successful.

A few follow-up questions:

  1. Is layouts/index.html the correct file to use?
  2. Should this code go somewhere else in the page?
  3. If I am only including one category (i.e., hide) should the second line of your code be edited in other ways? Does intersect need to be edited/removed?

Thank you so much for your help!

It needs to go above the {{ $paginator call and then should replace where .Site.Pages.ByDate.Reverse "Type" "post" with $filtered_posts.

@jsonbecker I’m almost there. Here’s how I understand what you just wrote:

    <div class="post_list" role="main">

    {{ $posts := where .Site.RegularPages "Type" "post" }}
    {{ $hidden_posts := where $posts "Params.categories" "intersect" (slice "hide") }}
    {{ $filtered_posts := $posts | symdiff $hidden_posts }}

        {{ $paginator := .Paginate (where .Site.Pages.ByDate.Reverse "Type" "post") }}

        {{ range .Paginator.Pages }}
        <div class="post-preview h-entry {{ range .Params.categories }} {{ . | urlize | lower }}{{ end }}">

I’m not sure how to replace the where statement with that code.

{{ $paginator := .Paginate $filtered_posts }}

It worked! Thank you, @jsonbecker. I’ve wanted to achieve this for a long time.

Is the same code also applicable for the archive in layouts/_default/list.archivehtml.html?

{{ define "main" }}
<div class="archive">
	<h2 class="p-name post-title">Archive</h2>
	{{ if templates.Exists "partials/microhook-archive-lead.html" }}
	{{ partial "microhook-archive-lead.html" . }}
	{{ end }}
	{{ $list := ($.Site.GetPage "taxonomyTerm" "categories").Pages }}
	{{ if gt (len $list) 0 }}
	<div class="archive-categories">
		<h3>Categories</h3>
		<div class="category-list">
			{{ range $list }}
			<a href="{{ .Permalink }}"><button>{{ .Title }}</button></a>
			{{ end }}
		</div>
	</div>
	{{ end }}
	<div class="full-archives h-feed">
		<h3>Full Post List</h3>
		{{ $list := (where .Site.Pages "Type" "post") }}
		{{ range $list }}

		<p class="h-entry">
			<a href="{{ .Permalink }}" class="u-url"><span class="dt-published" datetime="{{ .Date.Format "2006-01-02T15:04:05-0700" }}">{{ .Date.Format "Jan 2, 2006" }}</span></a>:
			{{ if .Title }}
			<span class="p-name"><b>{{ .Title }}</b></span>
			{{ end }}
			<span class="p-summary">{{ .Summary | truncate 150 }}</span>
		</p>

		{{ end }}
	</div>

</div>
{{ end }}

It’s a bit more complicated. One, you’ll want to do the setdiff trick with the $list to not have a link to hide.

Next you’ll want do the same trick to $list down after the Full Post List header.

Thank you @jsonbecker. You have been a great help.

I will try to see what I can do. I’ve made a few lunchtime attempts to use ChatGPT to accomplish hiding posts with the category hide on the archive page. One attempt hid all the posts, including those in categories I wanted displayed. Others resulted in no apparent changes.

Hi @willtmonroe - were you able to accomplish this with your archives as well? I was able to do this successfully with my homepage and wanting to remove the same category from my archive. Thanks!