如何使用 Timber\Post 从 ACF 扩展 WP post 对象数组
How to extend an array of WP post objects from ACF with Timber\Post
我正在使用 WordPress,Advanced Custom Fields, and Timber to build a theme. I am using an ACF relationship field to choose featured posts for one of my pages. This relationship field returns an array of WP post objects。
我使用以下代码获取此数组并将其添加到我的 Timber $context
:
/* featured posts */
$featured_posts = get_field('insights_featured_posts');
$context['featured_posts'] = $featured_posts;
我想要每一个WP post objects within this array to be extended using Timber/Post。我不确定使用 post 对象数组完成此操作的最佳方法。任何帮助将不胜感激。
这是我访问 $featured_posts
数组的 twig 文件:
{% 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 %}
你太接近了!
{% 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 %}
diff 是循环的开始 {% for post in Post(featured_posts) %}
此 Post()
方法会将数组转换为 Timber\Post
对象
我正在使用 WordPress,Advanced Custom Fields, and Timber to build a theme. I am using an ACF relationship field to choose featured posts for one of my pages. This relationship field returns an array of WP post objects。
我使用以下代码获取此数组并将其添加到我的 Timber $context
:
/* featured posts */
$featured_posts = get_field('insights_featured_posts');
$context['featured_posts'] = $featured_posts;
我想要每一个WP post objects within this array to be extended using Timber/Post。我不确定使用 post 对象数组完成此操作的最佳方法。任何帮助将不胜感激。
这是我访问 $featured_posts
数组的 twig 文件:
{% 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 %}
你太接近了!
{% 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 %}
diff 是循环的开始 {% for post in Post(featured_posts) %}
此 Post()
方法会将数组转换为 Timber\Post
对象