On a single page, I need to add an include, on the bottom of the page, that add 3 projects.
I have a content type called projects I made with ACF.
single-project.twig
I have a "single-project.twig" page that display the content of a post (which is also a project). At the bottom of this page, I need to add a block like this:
{% block related %}
{% include 'inc-related.twig' %}
{% endblock %}
inc-related.twig
Into "inc-related.twig", I have this:
{% for post in related_posts %}
{% if post.thumbnail %}
<a href="{{post.link}}" class="l-basicgrid-work work">
<article>
<figure>
<img data-src="{{post.get_thumbnail.src('medium_large')|resize(800, 533)}}" alt="{{post.title}}" class="lazy">
</figure>
<figcaption>
<h2>{{ post.title }}</h2>
</figcaption>
</article>
</a>
{% endif %}
{% endfor %}
related.php
I also create a "related.php" page, that integrate the following rendering:
$context = Timber::get_context();
$context['related_posts'] = Timber::get_posts('post_type=project&posts_per_page=3');
Timber::render('inc-related.twig', $context);
Questions
I have 2 questions:
- The project are not displaying into the single page. What I have done wrong?
- Can I select 3 project except the one who is displayed on the
single page?
Thank you
The ACF context is passed to the single page template via Timber
$context
, using theget_posts
function.single-project.php
Twig sends along the active context by default, so this template is fine as-is.
Finally, the posts in
$context['related_posts']
can be looped over. However, within thefor
loop, refer to each post by anything other thanpost
, such asrel_post
. This avoids conflict with the regularpost
object.