Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Get count of post according to value in frontmatter

Started by julien 6 years ago · 2 replies · 520 views
6 years ago

Hi,

I would like to know if it's possible to get the count of post according to their value in frontmatter.
Here is the context.
I made a recipe site for my family (amazing how much time you have in confinement 😄 ) .
I have a page that lists recipes.
Each recipe has a radio button field to set whether it's a starter, soup, salad, main dish or dessert.
I'm using this value with a js script to filter them on the front end. Everything works fine.
I'm simply trying to "echo" how many recipes for each value of recipe_type.

I know how to get the count of posts but not the count according to a variable.

I hope I was clear enough to describe my question.

Thank yu in advance.

6 years ago

If count is a variable in the frontmatter of your page like:
count: 3
then using that variable in your page requires the use of Twig on that page. To enable Twig processing of the page Markdown you need to add these lines to the page frontmatter:

YAML
process:
    twig: true
twig_first: true

I'm not entirely sure whether or not the twig_first: true is required. Try with and without it.

Then in your page content (Markdown) you can simply use {{ count }}.

6 years ago

Thank you @bleutzinn for your help.
In my case, I've actually realized that I should have used taxonomy to make more sense.
It's then easy to get a count.

Thanks again, it's appreciated.

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 195 2 months ago
Themes & Styling · by Ian, 2 months ago
3 91 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 451 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 45 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 125 3 months ago