首页 > 解决方案 > 如何使用 Timber\Post 从 ACF 扩展 WP po​​st 对象数组

问题描述

我正在使用 WordPress、Advanced Custom FieldsTimber来构建主题。我正在使用ACF 关系字段为我的一个页面选择精选帖子。此关系字段返回一个WP 帖子对象数组。

$context我使用以下代码抓取这个数组并将其添加到我的 Timber中:

/* featured posts */
$featured_posts = get_field('insights_featured_posts');
$context['featured_posts'] = $featured_posts;

我希望使用Timber/Post扩展此数组中的每个WP po​​st 对象。我不确定使用一组 post 对象来实现此目的的最佳方法。任何帮助,将不胜感激。

这是我访问$featured_posts数组的树枝文件:

{% for post in featured_posts %}
 <div class="col ns-col-is-4">
 <h3 class="post-heading">{{post.post_title}}</h3>
 <p class="post-content">{{post.post_excerpt}}</p>
 </div>
{% endfor %}

标签: phpwordpresstwigadvanced-custom-fieldstimber

解决方案


你这么近!

{% for post in Post(featured_posts) %}
  <div class="col ns-col-is-4">
    <h3 class="post-heading">{{ post.title }}</h3>
    <p class="post-content">{{ post.preview }}</p>
  </div>
{% endfor %}

差异是循环的开始{% for post in Post(featured_posts) %}Post()方法将该数组转换为Timber\Post对象


推荐阅读