首页 > 解决方案 > Wordpress,Timber,Twig 模板中 WCK 自定义字段的打印字段

问题描述

我已经使用 WCK 插件为自定义帖子类型创建了自定义字段,并希望将这些字段放入我的 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 }}但这些都没有在页面上打印任何内容。

这是完整的转储:

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)

标签: twigcustom-fieldstimber

解决方案


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

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

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

{{ home_page_content[0].question_1 }}

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


推荐阅读