【问题标题】:Wordpress, Timber, print field from WCK custom field in Twig templateWordpress,Timber,Twig 模板中 WCK 自定义字段的打印字段
【发布时间】:2020-06-21 04:25:24
【问题描述】:

我使用 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)

【问题讨论】:

  • 试试{{ home_page_content.question_1 }}
  • 可悲的是,对此也不满意 - 感谢 Henrik 的尝试!
  • 你能添加{{ dump(home_page_content) }}的完整输出吗?

标签: twig custom-fields timber


【解决方案1】:

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

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

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

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

这里的数字,在本例中为 0,表示您想要的元素。

【讨论】:

    猜你喜欢
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多