How To Exclude Specific Posts from the Home Page

Step 1: In the ghost admin, Tags, add a new Internal Tag #noindex. You will add this tag to any post or page that you want to exclude from appearing on the home page.

Step 2: In the Casper theme, in the index.hbs, edit the following code in between {{#foreach posts}} and {{/foreach}} like this…

        {{#foreach posts}}


            {{!-- exclude #noindex --}}
            {{#has tag="#noindex"}}
                {{!-- nothing --}}
            {{else}}
            {{!-- The tag below includes the markup for each post - partials/post-card.hbs --}}
            {{> "post-card"}}
            {{/has}}


        {{/foreach}}

So the Main Content Area section should now look like this:

{{!-- The main content area --}}
<main id="site-main" class="site-main outer">
<div class="inner posts">

    <div class="post-feed">
        {{#foreach posts}}


            {{!-- exclude #noindex --}}
            {{#has tag="#noindex"}}
                {{!-- nothing --}}
            {{else}}
            {{!-- The tag below includes the markup for each post - partials/post-card.hbs --}}
            {{> "post-card"}}
            {{/has}}


        {{/foreach}}
    </div>

</div>
</main>

I lifted this code from…

1 Like

For your specific use case, you should be able to simply use {{^has tag="#noindex"}} to select posts without the internal tag =)

1 Like

Hey @itsmingjie thank you for that!

I’ve been posting more on this forum over the holidays, and it’s been pretty quiet.

I was beginning to wonder if this REALLY WAS a “ghost” forum (like an abandoned “ghost ship” spookily adrift on the sea.)

So here’s how I applied that in my index.hbs…

{{#foreach posts}}

    {{!-- Exclude posts tagged #noindex --}}
    {{^has tag="#noindex"}}

            {{!-- The tag below includes the markup for each post - partials/post-card.hbs --}}
            {{> "post-card"}}
                
    {{/has}}

{{/foreach}}

4 Likes

I tried to get this working by ignoring multiple tags and without affecting the display styles of the styles in Casper (so at the foreach level). The best way I found it was to adding the filtering in the routes.yml:

In this way pagination will count all the excluded item as this logic is inside the foreach and if you have added 9 posts limit and your latest 9 items or 5 items comes with the excluded tag then only blank or 4 item will be display.

Hi Friends. Looking at the examples here I couldn’t get it to work because my homepage only should have 1 post on it. When excluding based on the tag the results were empty! Not exactly what you’d want.

Here is solution for getting all posts excluded by tag

    {{#get "posts" filter="tags:-hash-noindex" limit="1"}}
        {{#foreach posts}}
            <article class="gh-article {{post_class}}">
                {{> content}}
            </article>
        {{/foreach}}
    {{/get}}

Here is solution for getting only one result from a specific tag “changeme”

    {{#get "posts" filter="tags:[changeme]" limit="1"}}
        {{#foreach posts}}
            <article class="gh-article {{post_class}}">
                {{> content}}
            </article>
        {{/foreach}}
    {{/get}}
1 Like