Your sites Log in Sign up Menu

Remove certain tagged posts from #next

Hello. I have this block of code in entry.html. Is it possible to not show posts with a certain tag inside of next?

{{#next}}
<div class="up-next">
<p class="small small-caps last-updated">up next</p>
{{#thumbnail.medium}}
<div class="up-next-thumbnail">
<img src="{{{url}}}" width="256px">
</div>
{{/thumbnail.medium}}
<a href="{{{url}}}">{{title}}</a>
<p class="up-next-summary">{{summary}}…</p>
</div>
{{/next}}

2 months ago, 6 replies   developers   next   tags   Improve this question

I tried adding {{^tagged.stash}} and {{/tagged.stash}} and it didn't work unfortunately.

{{#next}}
{{^tagged.stash}}
<div class="up-next">
<p class="small small-caps last-updated">up next</p>
{{#thumbnail.medium}}
<div class="up-next-thumbnail">
<img src="{{{url}}}" width="256px">
</div>
{{/thumbnail.medium}}
<a href="{{{url}}}">{{title}}</a>
<p class="up-next-summary">{{summary}}…</p>
</div>
{{/tagged.stash}}
{{/next}}
Answered 2 months ago · Improve this answer

This is slightly tricky because of the context in which the {{^tagged.stash}} mustache template code is evaluated. Try the following which uses {{^next.tagged.stash}} instead:

{{^next.tagged.stash}}
{{#next}}
<div class="up-next">
<p class="small small-caps last-updated">up next</p>
{{#thumbnail.medium}}
<div class="up-next-thumbnail">
<img src="{{{url}}}" width="256px">
</div>
{{/thumbnail.medium}}
<a href="{{{url}}}">{{title}}</a>
<p class="up-next-summary">{{summary}}…</p>
</div>
{{/next}}
{{/next.tagged.stash}}
Answered 2 months ago · Improve this answer

Thank you so much. I just tried it and now the next area doesn't appear on the page. I assume that means there is a syntax error?

{{^next.tagged.stash}}
{{#next}}
<div class="up-next">
<p class="small small-caps last-updated">up next</p>
{{#thumbnail.medium}}
<div class="up-next-thumbnail">
<img src="{{{url}}}" width="256px">
</div>
{{/thumbnail.medium}}
<a href="{{{url}}}">{{title}}</a>
<p class="up-next-summary">{{summary}}…</p>
</div>
{{/next}}
{{/next.tagged.stash}}
Answered 2 months ago · Improve this answer

Out of curiosity I also tried

{{#next}}
{{^next.tagged.stash}}
{{/next.tagged.stash}}
{{/next}}
Answered 2 months ago · Improve this answer

Ah, I misunderstood your question: I thought you wanted to hide the 'next' link when the next post had a specific tag. It seems you actually want the 'next' link to be the next post without a specific tag, is that correct?

If so, it's not possible to customize the behaviour of the next/previous links like this at the moment at the template engine level.

Are you comfortable writing some javascript? If so, you could build a list of all your posts in JS, filter out those with a given tag, and then dynamically inject next/previous links. Something like this:

var posts = [
{{#allEntries}}
  {"title": "{{title}}", "tags": [{{#tags}}"{{tag}}",{{/tags}}], "url": "{{{url}}}"}
{{/allEntries}}
];

var removeStash = (post) => post.tags.includes('stash');
var currentPostIndex = posts.findIndex(post => post.url === window.location.pathname);
var nextPost = posts.slice(0, currentPostIndex).filter(removeStash).at(-1);
var previousPost = posts.slice(currentPostIndex).filter(removeStash).at(0);

Please note, the code above is just an illustration of a possible technique, it'll need to be properly implemented to work

Answered 2 months ago · Improve this answer

Oh sorry! I wasn't clear. This is not about the next link. I was talking about the ability to show a preview of the next blog post on a blog post page. So in the preview area don't show posts with tag "stash." But I'm assuming based on your comment that this is not possible.

Answered 2 months ago · Improve this answer