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

List of recent news/events from blog

Solved by pamtbaau View solution

Started by Michael Schlottke-Lakemper 4 years ago · 1 replies · 790 views
4 years ago

We are trying to use the blog feature as an readily available mechanism to store "news" items (event announcements, information on papers published etc.). On our homepage, we would thus like to show a short list of say, the latest 5-6 posts with just the title and maybe a date, similar to this
image|690x289

Specifically, I want this list to appear on a website with several other static and dynamic items.

Is there an easy way to achieve this with Grav? And is it possible to do it just in Markdown (such that it can be easily positioned in relation to other entries on a page) or would I have to create a special HTML template for it? I've read about twigs being able to do something like this (e.g., https://learn.getgrav.org/17/cookbook/twig-recipes#list-the-last-5-recent-bl), but I am too unfamiliar with Grav/Twigs yet to know exactly how and where I can use such a code.

4 years ago Solution

@MichaelSL,

Is there an easy way to achieve this with Grav?

Sure. See how a blog is build in skeleton Blog Site. The blog/news collection does not need to be shown by the Blog page itself. The collection defined in blog.md, can be accessed from any Twig template and sliced and looped through.

TWIG
{% set news = page.find('/news').collection() %}

{% if news|length > 0 %}
  <ul>
    {% for item in news %}
      {# display blog items #}
    {% endfor %}
  </ul>
{% endif %}

And is it possible to do it just in Markdown (such that it can be easily positioned in relation to other entries on a page) [..]

Markdown isn't created to 'position' entries. It is very simple tool to create content.
Twig snippets van be embedded into Markdown. However, I wouldn't embed Twig inside Markdown. Markdown is created to edit content. Twig is being created to design page layout.

There is also is possibility of using so called 'shortcodes' with which Markdown can be 'enriched'. Not sure if the is a shortcode plugin that allows the listing of a collection though.

[..] or would I have to create a special HTML template for it?

I think that would be the best option. And templates can easily be reused and included in Twig or Markdown.

Suggested topics

Topic Participants Replies Views Activity
Support · by Water Science, 3 weeks ago
11 172 9 hours ago
Support · by Duc , 1 day ago
1 46 1 day ago
Support · by Thomas, 1 week ago
3 116 1 day ago
Support · by Anna, 4 days ago
2 111 2 days ago
Support · by Justin Young, 2 days ago
1 91 2 days ago