Issue with Post Object Timber/Twig WordPress

♀尐吖头ヾ 提交于 2021-02-11 13:54:07

问题


I have an ACF field, It's a Repeater, and inside the Repeater is a Post Object.

Controller

/* Template Name: Strategic Partners */

use Timber\Timber;
use Timber\PostQuery;
use Flynt\Utils\Options;

use const Flynt\Archives\POST_TYPES;

$context = Timber::get_context();

$context['slider'] = get_field('slider');
$context['featured'] = get_field('featured');

if (isset($_GET['contentOnly'])) {
    $context['contentOnly'] = true;
}

Timber::render('templates/StrategicPartners/index.twig', $context);

Here's the snippet from the View

{% for partner in Post(featured) %}
  {{ partner.link }}
{% endfor %}

I'm var_dumping the context, and it's getting the Timber Post... but {{ partner.title }} does not grab the Post Title, it's blank.

{{ partner.link }} comes out with the direct link.

Output

http://***/strategic-partners/
http://***/strategic-partners/

What am I doing wrong here? I'm using {{ Post() }}} within a seperate repeater, within for foreach itself and that works on another page, but it does not work here either, so I tried Post() included in the foreach and it still is not working.


回答1:


No one answered this for me. I answered it myself.

There isn't much documentation on this within the formal Timber documentation so I hope this helps somebody.

Do not call Post with your foreach

Do this for a Post Object within a Repeater (ACF)

{% for item in seq %}
 {{ Post(item.post_object_name).field }}
{% endfor %}

seq is your ACF Field

post_object_name is the Post Object within the field.

field is the field within the Post Object type.

Hope this helps someone.

For more Context:

Controller

<?php

/* Template Name: Strategic Partners */

use Timber\Timber;
use Timber\PostQuery;
use Flynt\Utils\Options;

use const Flynt\Archives\POST_TYPES;

$context = Timber::get_context();

$context['slider'] = get_field('slider');
$context['featured_sps'] = get_field('featured_strategic_partners');
$context['rebate'] = get_field('rebate_information');

if (isset($_GET['contentOnly'])) {
    $context['contentOnly'] = true;
}

Timber::render('templates/StrategicPartners/index.twig', $context);

Twig View

{% for partner in featured_sps %}
  {{ Post(partner.featured).title }}
{% endfor %}

Images are more complex

{% for partner in featured_sps %}
  <div class="slide">
    <a href="{{ Post(partner.featured).link }}">
      <img src="{{ Image(Post(partner.featured).logo).src }}" alt="{{ Post(partner.featured.title) }} Logo">
    </a>
  </div>
{% endfor %}


来源:https://stackoverflow.com/questions/61805121/issue-with-post-object-timber-twig-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!