Add posts with timber twig into an include

1.3k views Asked by At

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

3

There are 3 answers

0
Hannah On

The ACF context is passed to the single page template via Timber $context, using the get_posts function.

single-project.php

$context['related_posts'] = Timber::get_posts('post_type=project&posts_per_page=3');

Twig sends along the active context by default, so this template is fine as-is.

{% block related %}
  {% include 'inc-related.twig' %}
{% endblock %}

Finally, the posts in $context['related_posts'] can be looped over. However, within the for loop, refer to each post by anything other than post, such as rel_post. This avoids conflict with the regular post object.

{% for rel_post in related_posts %}
  {% if rel_post.thumbnail %}
    ...
  {% endif %}
{% endfor %}
1
Maxim Sarandi On

you have include twig template but not send data to him. Your related.php is not include automatically. You should write $context['related_posts'] = Timber::get_posts('post_type=project&posts_per_page=3'); in your single-project.twig. And do not use post name in inc-related.twig. May be conflict with current post object.

To single-project.php add $context['related_posts'] = Timber::get_posts('post_type=project&posts_per_page=3');

0
filnug On

This is what I did.

single-project.php

I have created a single-project.php page. It contains the following script:

$context = Timber::get_context();

$args = array(
    // Get post type project
    'post_type' => 'project',
    // Get 3 posts
    'posts_per_page' => 3,
    // Order random
    'orderby' => 'rand',
);

$context['related_posts'] = Timber::get_posts( $args );
Timber::render('single-project.twig', $context);

single-project.twig

On my single-project.twig, I have the code that get the result of the php page. Actually, that code is included into an "inc-related.twig" page. And it works!

{% 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 %}

But on the same page (single-project.twig), I also have data that display content from Advanced Custom Field.

                    {% if post.project_images %}
                        {% for item in post.get_field('project_images') %}  
                            <figure class="{{item.project_media_css}}">
                                <img data-src="{{TimberImage(item.project_media).src}}" alt="{{item.alt_media}}" class="lazy">
                            </figure>  
                        {% endfor %}
                    {% endif %} 

The problem now is that the ACF field don't return anything.

How should I modify single-project.php to get content from "related_project" and ACF field?