I’m building a template for my portfolio website.
Oddly there’s no post showing up on mywebsite.com/work/ page.
The followings are my settings which might be related to this issue.
I checked the HTML of the page but there {{#foreach posts}} to {{/foreach}} area doesn’t seem to be working.
It would be great if you could point out some issue(s) I might overlook.
routes.yaml
routes
/: home
collections:
/blog/:
permalink: /blog/{slug}/
template: index
/work/:
permalink: /work/{slug}/
template: portfolio
filter: tag:work
data: tag.work
Could you try removing the line data: tag.work from your routes file, and removing {{#has tag="work"}} and {{/has}} from your portfolio template. Data isn’t needed and the has helper isn’t required as the filter value in the routes file is doing that for you
Edit: Sorry but I’ve just realised that you might be using the tag description and title for the page data. Please ignore the routes file change if you are doing this
I found the solution for this issue so I want to share it with you and the community.
It turned out that I didn’t add #get handlebar on portfolio.hbs.
Also, I added visibility="all" to #foreach as I use membership access controls for some posts.
{{!-- The main content area on the portfolio page --}}
<div class="content-area">
<main class="site-main">
<div class="post-feed">
<div class="grid-item grid-sizer"></div>
{{#get "posts" include="tags" limit=@config.posts_per_page}}
{{!-- The tag below includes the portfolio loop - partials/portfolio-loop.hbs --}}
{{#foreach posts visibility="all"}}
{{#has tag="work"}}
{{> "portfolio-loop"}}
{{/has}}
{{/foreach}}
{{/get}}
{{pagination}}
</main><!-- .site-main -->
</div><!-- .content-area -->
I didn’t come up with that idea without your suggestion, @DavidDarnes.
After I tried your method and found out that was not it, I could get a clue where I should look into next.
It’s a bit strange for me that I needed to use #get handlebar, because on another theme, it worked solely with #foreach to call a post loop.
That’s great you got it working, and that I was able to help! However you are right, you shouldn’t need the #get helper here if you’re already using a custom collection and putting a filter on it . Maybe something else in the code is affecting it?
Ghost will see the /blog/ collection first and because there’s no filter, all of your posts will be added to that collection. When it comes to the /work/ collection, there are no posts left to put into to it.
To comply with the restriction you’d need to add a filter to your main /blog/ collection so that your work posts don’t get included there, eg:
If you do that then you should be able to get rid of the {{#get}} helper and the {{#has tag="work"}} conditional in your portfolio.hbs template.
Note, that if you’re using the {{#get}} helper in the way you’re currently doing it’s not fixing the collection problem - pagination won’t function and your “work” posts will continue to live on /blog/{slug}/ rather than the /work/{slug}/ URLs that you’ve specified.
Thank you for providing a better solution, @Kevin!
I applied that change to my theme accordingly and it’s working!
Also thank you so much for saving my time. With my solution, I would have run into the pagination issue you mentioned.