Use a grid layout for posts tagged “photos”?
I'd like to add a grid of photos for posts with the tag 'photos' rather than the chronological list of posts. How do I accomplish this?
Let’s assume you’re using the Blog template and you want posts with a specific tag (for example, photos) to use a different layout than the default tagged list. You can do this by splitting the layout logic into two partial templates and conditionally including them.
1. Create a grid layout for one tag
Open the template editor, choose Edit code, and create a new template file called tagged-grid.html. Paste the following into it:
<div class="photo-grid">
{{#entries}}
<a class="photo-item" href="{{{url}}}">
<div class="photo-thumb">
{{#thumbnail.small}}
<img
src="{{{url}}}"
width="{{width}}"
height="{{height}}"
loading="lazy"
alt="{{title}}"
>
{{/thumbnail.small}}
</div>
<div class="photo-meta">
<span>{{title}} {{date}}</span>
</div>
</a>
{{/entries}}
</div>
<style>
.photo-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
gap: 14px;
align-items: start;
}
.photo-item {
display: block;
text-decoration: none;
color: inherit;
}
.photo-thumb {
background: rgba(0,0,0,0.04);
box-shadow: 0 1px 2px rgba(0,0,0,0.06);
transition: transform 120ms ease, box-shadow 120ms ease;
}
.photo-thumb img {
display: block;
width: 100%;
height: auto;
}
.photo-meta {
margin-top: 8px;
font-size: 0.92rem;
line-height: 1.25;
opacity: 0.75;
}
</style>
This template renders tagged entries as a responsive image grid. It will be used only for the tag you choose (in this example, photos).
2. Create a fallback list layout
Next, create another template file called tagged-list.html. This will be used for all other tags.
<div class="entry">
{{#entries}}
<a href="{{{url}}}">{{title}}</a>
<span class="light">{{date}}</span>
<br>
{{/entries}}
</div>
This preserves the standard, simple list layout for non-photo tags.
3. Update tagged.html to select the layout
Finally, open the existing tagged.html file. Replace lines 10–18 with the following conditional logic:
{{#tagged.is.photos}}
{{> tagged-grid}}
{{/tagged.is.photos}}
{{^tagged.is.photos}}
{{> tagged-list}}
{{/tagged.is.photos}}
This checks whether the current tag is photos. If it is, Blot includes tagged-grid.html; otherwise, it falls back to tagged-list.html.
Please let me know if you have any questions about this or anything else
This is great. I also have the desire to have the grid present on a page vs. a post. Is that something that can be achieved by having that page also have the photos tag?
I don't think so – the thumbnail for the page will appear in the grid if you add this tag to a page. Is your goal to add some content above the grid, sourced from a file in your folder? e.g. an introductory paragraph?
No. My goal is that while my site serves primary as a blog, I'd like to have a page that serves as a photo gallery of sorts. I would sometimes have a post that would be tagged 'photos' that serve as a collection, and then the gallery page would be where I could curate a photo grid that links to those posts, but it would look very cohesive to have the same grid style.
I hope that makes sense.
After posting the above, I realize I may just need to handle the gallery page as html to achieve what I'm after (vs. a markdown page and putting it on the parser to try and achieve it).
I’m not quite sure I fully understand the desired structure yet. Feel free to email me with the specifics on your site.
If you'd like to create an image gallery within a single post (markdown file), you could use a fenced div to style a list of images into a gallery, e.g.
:::gallery
- 
- 
- 
- 
:::
This can then be styled in your template:
.gallery{
display:grid;
grid-template-columns:repeat(auto-fit,minmax(160px,1fr));
gap:12px;
}
.gallery img{
width:100%;
height:auto;
display:block;
}
I'm also working on a feature to support multi-file posts which would make this even easier.
This is great, thank you. The fenced div is actually the better solution for my current needs, but the multi-file posts will be a nice addition so that I can just throw image files into a folder that tags files and then have those generate posts that are collections.