Hello, I’ve begun experimenting with Grav and perhaps this is a basic question since I haven’t been able to find a similar question here.
I’d like to display an image in a sidebar based on whatever name is present in a (blog) post’s taxonomy category.
In other words, suppose I have 20 blog posts that each include a category called Dog, then whenever someone looks at one of those posts, the same image associated with Dog would display.
Certainly, here I’ll explain what I did below. One caveat though, I’m certainly no professional and I am fairly certain that there are more elegant/better ways to accomplish what I did. It is working for what I wanted though.
I’ve been extending the Gateway theme and in that theme there is a sidebar.html.twig that is useful for showing various things in a sidebar blog-like format.
As mentioned, I wanted to show an image that is associated with a category, in that sidebar, depending on what is being browsed next to it.
In most cases that would probably be a blog post (which would have 1 or more categories–though I only want the first) but it could also be if someone were to click on the category name to see all posts in the category. Furthermore, I’ve organized my site in 4 main topical sections (these are top level pages, with many child pages). Those sections do not have categories (because they’re at a “higher” hierarchy concept level) but they each have an icon that I want to appear in the place where a category image would otherwise have appeared in the sidebar. I added a metadata.section to those pages to help me distinguish them.
So, I put the following in my sidebar.html.twig and made a new template in partials/catimageassoc.html.twig that I use to associate each category (or section) with a specific image.
Instead of <img src="../user/themes/mytheme/img/catimage/firstname.png"></img>, you could use:
<img src="/user/themes/quark/images/favicon.png" >
or
<img src="{{ url('theme://images/favicon.png') }}" >
or
{{ page.media['theme://images/favicon.png'].html | raw }}