木材:以自定义帖子类型呈现特色图像

问题描述

我发现此问题之前已在 Timber's repo标记,但在 2021 年,此解决方案似乎对我不起作用。我正在尝试为自定义帖子类型呈现特色图像,该类型本身是通过 ACF 转发器字段呈现的。

在 Twig 模板中,我能够毫无问题地呈现自定义帖子类型(即 ID、post_title)的其他组件:

{% for hosting in hosting_providers %}
    {% set hosting_image = TimberImage(hosting.thumbnail) %}
        <tr class="domain-connector__tool-form-hosting">
            <td class="border-none">
                <label>
                    <table>
                        <tr>
                            <td class="border-none"><input type="radio" name="hosting-provider" value={{ hosting.ID }} class="hosting-provider__input"/></td>
                            <td class="border-none"><img src="{{ hosting_image.src|resize('full') }}"></td>
                            <td class="border-none"><p>{{ hosting.post_title }}</p></td>
                        </tr>
                    </table>
                </label>
            </td>
        </tr>
{% endfor %}

有趣的是,当我尝试使用 var_dump 进行调试时,似乎没有我可以访问的自定义帖子类型的缩略图

object(WP_Post)[3942]
  public 'ID' => int 2372
  public 'post_author' => string '1' (length=1)
  public 'post_date' => string '2021-01-28 23:01:04' (length=19)
  public 'post_date_gmt' => string '2021-01-28 23:01:04' (length=19)
  public 'post_content' => string '' (length=0)
  public 'post_title' => string 'Namecheap' (length=9)
  public 'post_excerpt' => string '' (length=0)
  public 'post_status' => string 'publish' (length=7)
  public 'comment_status' => string 'open' (length=4)
  public 'ping_status' => string 'open' (length=4)
  public 'post_password' => string '' (length=0)
  public 'post_name' => string 'namecheap' (length=9)
  public 'to_ping' => string '' (length=0)
  public 'pinged' => string '' (length=0)
  public 'post_modified' => string '2021-01-30 10:49:36' (length=19)
  public 'post_modified_gmt' => string '2021-01-30 10:49:36' (length=19)
  public 'post_content_filtered' => string '' (length=0)
  public 'post_parent' => int 0
  public 'guid' => string 'http://websitetooltesternew.local/blog/wtt_objects/namecheap/' (length=61)
  public 'menu_order' => int 0
  public 'post_type' => string 'wtt_objects' (length=11)
  public 'post_mime_type' => string '' (length=0)
  public 'comment_count' => string '0' (length=1)
  public 'filter' => string 'raw' (length=3)

这可以解释为什么没有渲染。但我的问题是,特色图片去了哪里?我正在 Timber 环境中重写当前主题,并且在原始主题PHP/HTML 意大利面代码)中似乎可以正常渲染。

解决方法

帖子数据是否在 Timber\Post 中?

这是您用来访问或扩展 WordPress 帖子的对象。可以将其视为 Timber(更易于访问)的 WP_Post 版本。

例如:

$context = Timber::context();

$args = array(
    'post_type' => 'custom_post_type',);

$context['custom_post_type'] = Timerb:get_posts($args);
Timber::render('view.twig',$context);

您可以使用 {{ dump( post ) }}(启用调试)进行检查,它会在输出中显示 Timber\Post。