Pull feature post to home page

First, thank you the author of Zola who creates a so great software.
I’m new to this and still learning.

When creating a homepage, is there a way to pull the title, & partial content of certain post (based on date, or name…)

\content
|->\post
|->\post\post1.md
|->\post\post2.md

is there a way to put the title & and partial content of post1.md or post2.md to the siteroot\index.html?

I know I can render the content if I have it in the _index.md. but if it can pull directly from the post, that’ll be great.

Something like “last news” or “feature story”…

Thank you again for any help.

Hi @Ky2X1j8Tkrh,

if you keep all your posts in the post folder, there’s an easy solution.

{% set post_section = get_section(path="post/_index.md") %}
{% for post in post_section.pages %}
  <div><a href="{{post.permalink}}">{{post.title}}</a></div>
{% endfor %}

the get_section function gets a section object. You can use that to retrieve all pages direct in the post folder.

This example just generates a list of of links to the posts, but you have access to all variables of a page object.

Also have a look at the functions available in tera, that’s the template engine used by zola, You can use these to apply filter to the section.pages list and only print those that you want on your homepage.

I hope this helps you get started.

2 Likes

Thank you @Marco, I’ll try that.

No problem,

if you have more questions, just ask.

in the

the section “post” was hard coded in.

If I want to set it in the config file like

[extra]
featureSection = “post”

then use it like
{% set post_section = get_section(path="{{config.extra.featureSection}}/_index.md") %}

it’ll error. is there a proper way to do this?

Sorry can not find the get_section doc online.

Thank you.

Hi @Ky2X1j8Tkrh,

Yes there is a way to do this without hardcoding the section.
You were almost at the right solution.

When you look at the code here:

{% set post_section = get_section(path=config.extra.featureSection ~ "/_index.md") %}
{% for post in post_section.pages %}
  <div><a href="{{post.permalink}}">{{post.title}}</a></div>
{% endfor %}

You don’t need the braces {{config.extra.featureSection}}, because you are already inside a block using {% %} style braces.

The second change is that you need to concatenate the string value using the ~ operator. That’s the way strings are concatenated in the tera template engine.

Does this anwser your question?

Marco

Thanks @Marco again. it works.