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
you have include twig template but not send data to him. Your
related.phpis not include automatically. You should write$context['related_posts'] = Timber::get_posts('post_type=project&posts_per_page=3');in yoursingle-project.twig. And do not usepostname ininc-related.twig. May be conflict with current post object.To
single-project.phpadd$context['related_posts'] = Timber::get_posts('post_type=project&posts_per_page=3');