Skip to main content
6 of 8
deleted 15 characters in body; edited title
Andrea
  • 1.3k
  • 2
  • 15
  • 28

Display latest three news on a custom template

I'm building a custom theme and I would like to display last third inserted news (i.e. nodes with content type = News) on the bottom of the home page from inside the home page template.

Something like this:

+-------------------------------+
| +---------------------------+ |
| |                           | |
| |          Slides           | |
| |                           | |
| +---------------------------+ |
|                               |
| +---------------------------+ |
| |                           | |
| |           Page            | |
| |           Body            | |
| |                           | |
| |                           | |
| +---------------------------+ |
|                               |
| +-------+ +-------+ +-------+ |
| | Last  | |Second | | Third | |
| | News  | | Last  | | Last  | |
| |       | | News  | | News  | |
| +-------+ +-------+ +-------+ |
+-------------------------------+

I created a content type named Home page, with fields for manage slides and the page body, then I created a new content (a new node) of type Home page.

Inside my custom theme I created a template file named node--home-page.html.twig and inside it I can handle the Home page's fields (slides and body) and how to render them inside the page.

After that I created another content type News and I added some content of such type.

Now I would like to display latest news inside the homepage template as in the figure above. What is the better/simplest way to do it?

I'm thinking to load latest news in some hook_preprocess_HOOK function (using the EntityFieldQuery object) then add them inside a variable for the Twig template. Inside the template I will perform a for loop rendering latest news at the page's bottom. Is this a good way to do it or I'm completely out of the way?


EDIT

Correction: I would like to display latest news between slides and body, in this way:

+-------------------------------+
| +---------------------------+ |
| |                           | |
| |          Slides           | |
| |                           | |
| +---------------------------+ |
|                               |
| +-------+ +-------+ +-------+ |
| | Last  | |Second | | Third | |
| | News  | | Last  | | Last  | |
| |       | | News  | | News  | |
| +-------+ +-------+ +-------+ |
|                               |
| +---------------------------+ |
| |                           | |
| |           Page            | |
| |           Body            | |
| |                           | |
| |                           | |
| +---------------------------+ |
+-------------------------------+

Where slides and body are two fields of the Home page content type.

Andrea
  • 1.3k
  • 2
  • 15
  • 28