Post Object Timber/Twig WordPress 的问题 [英] Issue with Post Object Timber/Twig WordPress

查看:26
本文介绍了Post Object Timber/Twig WordPress 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ACF 字段,它是一个中继器,中继器内部是一个 Post 对象.

控制器

/* 模板名称:战略合作伙伴 */使用木材\木材;使用 Timber\PostQuery;使用 Flynt\Utils\Options;使用 const Flynt\Archives\POST_TYPES;$context = 木材::get_context();$context['slider'] = get_field('slider');$context['featured'] = get_field('featured');如果(isset($_GET['contentOnly'])){$context['contentOnly'] = true;}Timber::render('templates/StrategicPartners/index.twig', $context);

这是视图

的片段

{% for Partner in Post(Featured) %}{{ 合作伙伴.link }}{% 结束为 %}

我正在var_dump处理上下文,它正在获取Timber Post...但是{{partner.title }} 没有获取帖子标题,它是空白.

{{ partner.link }} 带有直接链接.

输出

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

我在这里做错了什么?我在单独的转发器中使用 {{ Post() }}} ,在 for foreach 本身内使用,并且可以在另一个页面上使用,但它在这里也不起作用,所以我尝试了包含在 foreach 中的 Post() 但它仍然无法正常工作.

解决方案

没有人为我回答这个问题.我自己回答了.

在正式的 Timber 文档中没有太多关于此的文档,所以我希望这对某人有所帮助.

不要用你的 foreach 调用 Post

对中继器 (ACF) 中的 Post 对象执行此操作

{% for item in seq %}{{ Post(item.post_object_name).field }}{% 结束为 %}

seq 是您的 ACF 字段

post_object_name 是字段内的 Post 对象.

field 是 Post Object 类型中的字段.

希望这对某人有所帮助.

更多上下文:

控制器

树枝视图

{% for partner in features_sps %}{{ 帖子(partner.featured).title }}{% 结束为 %}

图像更复杂

{% for partner in features_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>

{% 结束为 %}

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.

解决方案

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

这篇关于Post Object Timber/Twig WordPress 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆