为什么这个ACF字段无法正确更新?

问题描述 投票:0回答:1

我正在使用 Timber 和 Twig 来构建我目前正在开发的 Wordpress 网站,但(对我来说)陷入了 ACF 领域相当奇怪的行为。

ACF 字段是一个简单的真/假复选框,在我的模板中,我想根据字段值进行条件渲染,这是有效的。但是当我更新 wp-admin 中的 ACF 字段时,该帖子就不会呈现。

我正在获取特定帖子类型的所有帖子,并在模板中检查帖子上的 ACF 值。例如,如果我创建一个新帖子并设置 ACF 字段,它可以工作,但在更改它时,它不会渲染。我在这里做错了什么?

PHP

$context = Timber::context();

$context['coworkers'] = Timber::get_posts([
  'post_type' => 'coworker'
]);

$templates = array('page/coworkers/index.twig');

Timber::render($templates, $context);

树枝

{% if coworkers %} // list of posts

  {% for item in coworkers %}

    {% set coworker = item.meta('coworker') %} // 1 or 0

    {% if coworker %}
      <div>
          coworker
      // its a coworker
      </div>
    {% else %}
      <div>
          something
      // its something else
      </div>
    {% endif %}

  {% endfor %}

{% endif %}
wordpress twig timber
1个回答
0
投票

以下 php 中的解决方案限制查询,而不必在模板中使用 if/else。

$context = Timber::context();

$context['coworkers'] = Timber::get_posts([
  'post_type' => 'coworker',
  'meta_query' => array(
      array(
          'key'   => 'coworker',
          'value' => '1',
      )
  )
]);

$templates = array('page/coworkers/index.twig');

Timber::render($templates, $context);
© www.soinside.com 2019 - 2024. All rights reserved.