Wordpress,木材,Twig模板中WCK自定义字段的打印字段

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

我已经使用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') }},我也尝试了使用{{ post.meta('are_you').question_1 }}遵循ACF的建议,但是这些都不在页面上打印任何内容。

这里是完整的转储:

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)
twig custom-fields timber
1个回答
0
投票

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

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

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

{{ home_page_content[0].question_1 }}

其中数字(在这种情况下为0,指您想要的元素。

© www.soinside.com 2019 - 2024. All rights reserved.