php - 如何使用 Timber\Post 从 ACF 扩展 WP post 对象数组
问题描述
我正在使用 WordPress、Advanced Custom Fields和Timber来构建主题。我正在使用ACF 关系字段为我的一个页面选择精选帖子。此关系字段返回一个WP 帖子对象数组。
$context
我使用以下代码抓取这个数组并将其添加到我的 Timber中:
/* featured posts */
$featured_posts = get_field('insights_featured_posts');
$context['featured_posts'] = $featured_posts;
我希望使用Timber/Post扩展此数组中的每个WP post 对象。我不确定使用一组 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 %}
解决方案
你这么近!
{% 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
对象
推荐阅读
- angular - Angular 和 Typescript - 在 contenteditable div 中设置 cursur 位置多个子元素(尝试了几乎所有文章)
- docker - 使用 Singularity 时如何挂载主机文件
- c# - DataAnnotations TryValidateProperty 看不到属性
- spacy - 我可以使用带标签的数据和基于规则的匹配来使用 Spacy 进行多类文本分类吗?
- python-3.x - discord.py 我的静音命令会阻止其他命令
- laravel - Laravel 5.8 JWT-token 与多个表
- c# - Knockout.js 将参数绑定到 Data-Target 返回对象而不是文本(手风琴)
- c# - 我可以使用另一个自定义环境变量名称而不是 GOOGLE_APPLICATION_CREDENTIALS 吗?
- indexing - 乳胶数字环境:如何从固定数字开始对数字进行索引?
- java - 如果字符是 0 到 8 之间的数字,则将其加 1(其他字符不变)