Twig 模板中 WCK 自定义字段的 Wordpress、Timber、打印字段

Wordpress, Timber, print field from WCK custom field in Twig template

我已经使用字段的 WCK 插件为自定义 post 类型创建了自定义字段,并希望将这些字段添加到我的 Timber Twig 模板中。 (我知道 Timber 为此推荐 ACF,但我首先尝试 WCK,因为这是一个非营利性网站,WCK 在其免费插件版本中提供了 ACF 没有的东西)。这是 php 文件的相关行:

$context['home_page_content'] = Timber::get_posts('post_type=vopthomepage');

在我的树枝模板中打印:

{{ dump(home_page_content) }}

我得到的相关部分(我想?)是:

public 'object_type' => string 'post' (length=4)
public 'post_type' => string 'vopthomepage' (length=12)
public 'are_you' => // this is the field group
public 'question_1' => string 'New to the area, looking for a church to join?' (length=46) // this is the field

所以为了获得我试过的字段 {{ post.question_1 }} ,没有快乐,所以然后尝试 {{ post.get_field('question_1') }} 我也尝试按照 ACF 的建议使用 {{ post.meta('are_you').question_1 }} 但 none这些打印页面上的任何内容。

这是完整转储:

array (size=1)
  0 => 
    object(Timber\Post)[5652]
      public 'ImageClass' => string 'Timber\Image' (length=12)
      public 'PostClass' => string 'Timber\Post' (length=11)
      public 'TermClass' => string 'Timber\Term' (length=11)
      public 'object_type' => string 'post' (length=4)
      public 'custom' => 
        array (size=7)
          '_edit_last' => string '1' (length=1)
          '_edit_lock' => string '1583704531:1' (length=12)
          'are_you' => 
            array (size=1)
              ...
          '_rsc_content_availability' => string 'everyone' (length=8)
          '_rsc_capability' => string '' (length=0)
          'question_1' => string 'New to the area, looking for a church to join?' (length=46)
          'answer_1' => string 'Take a look under the What we do menu, as well as Churches for info and the locations of buildings.' (length=99)
      protected '___content' => null
      protected '_permalink' => null
      protected '_next' => 
        array (size=0)
          empty
      protected '_prev' => 
        array (size=0)
          empty
      protected '_css_class' => null
      public 'id' => int 293
      public 'ID' => int 293
      public 'post_author' => string '1' (length=1)
      public 'post_content' => string '' (length=0)
      public 'post_date' => string '2020-03-04 16:42:52' (length=19)
      public 'post_excerpt' => string '' (length=0)
      public 'post_parent' => int 0
      public 'post_status' => string 'publish' (length=7)
      public 'post_title' => string 'Home page content' (length=17)
      public 'post_type' => string 'vopthomepage' (length=12)
      public 'slug' => string 'help' (length=4)
      protected '__type' => null
      public '_edit_last' => string '1' (length=1)
      public '_edit_lock' => string '1583704531:1' (length=12)
      public 'are_you' => 
        array (size=1)
          0 => 
            array (size=2)
              ...
      public '_rsc_content_availability' => string 'everyone' (length=8)
      public '_rsc_capability' => string '' (length=0)
      public 'question_1' => string 'New to the area, looking for a church to join?' (length=46)
      public 'answer_1' => string 'Take a look under the What we do menu, as well as Churches for info and the locations of buildings.' (length=99)
      public 'post_date_gmt' => string '2020-03-04 16:42:52' (length=19)
      public 'comment_status' => string 'closed' (length=6)
      public 'ping_status' => string 'closed' (length=6)
      public 'post_password' => string '' (length=0)
      public 'post_name' => string 'help' (length=4)
      public 'to_ping' => string '' (length=0)
      public 'pinged' => string '' (length=0)
      public 'post_modified' => string '2020-03-08 21:08:08' (length=19)
      public 'post_modified_gmt' => string '2020-03-08 21:08:08' (length=19)
      public 'post_content_filtered' => string '' (length=0)
      public 'guid' => string 'https://mbp-2.local:5757/?post_type=vopthomepage&p=293' (length=65)
      public 'menu_order' => int 0
      public 'post_mime_type' => string '' (length=0)
      public 'comment_count' => string '0' (length=1)
      public 'filter' => string 'raw' (length=3)
      public 'status' => string 'publish' (length=7)

您的内容在 home_page_content 中,它是 Timber\Post 个对象的数组。要获取内容,您必须像这样遍历它们:

{% for post in home_page_content  %}
  {{ post.question_1 }}
{% endfor %}

或者,如果您只需要数组中单个特定元素的数据,您可以执行类似

的操作
{{ home_page_content[0].question_1 }}

其中数字(在本例中为 0)指的是您想要的元素。