如何解决WordPress-如何在循环中用木材TWIG连接变量
我在wordpress中使用Timber(TWIG)。
我正在尝试创建一个具有两个不同帖子列表的标签系统。我正在尝试获取每个列表的帖子数(自定义帖子类型视频和自定义帖子类型播客)。
示例:视频(3) /播客(12)
我创建了一个循环,以获取每种自定义帖子类型(视频和播客)的帖子列表。
category.PHP
$object_category = get_queried_object();
$current_category = $object_category->term_id;
$posts_types_selected = array('videos','podcasts');
$context['posts_types_selected'] = $posts_types_selected;
foreach ($posts_types_selected as $post_type_selected) {
$context['posts_'.$post_type_selected] = Timber::get_posts(array(
'post_type' => array($post_type_selected),'post_status' => 'publish','category__in' => array($current_category),'posts_per_page' => 10,'paged' => 1,'has_password' => FALSE
));
}
category.twig
<ul class="tab-menu">
{% for item in posts_types_selected %}
<li><a href="#" class="tab-control">{{ item }} ({{ 'posts_' ~ item|length }})</a></li>
{% endfor %}
</ul>
我正在尝试为每个li
<li><a href="#" class="tab-control">{{ item }} ({{ posts_videos|length }})</a></li>
<li><a href="#" class="tab-control">{{ item }} ({{ posts_podcasts|length }})</a></li>
如何将数组$posts_types_selected
中的值与posts_
串联
更新
我在这里尝试了解决方法:How to access dynamic variable names in twig?
{% for item in posts_types_selected %}
<li><a href="#" class="tab-control">{{ item }} ({{ _context['posts_' ~ item] }})</a></li>
{% endfor %}
但是我得到了:Notice: Array to string conversion
我也尝试过:
{% for item in posts_types_selected %}
<li><a href="#" class="tab-control">{{ item }} ({{ 'posts_' ~ item }})</a></li>
{% endfor %}
我得到了:videos (posts_videos)
/ podacasts (posts_podcasts)
但是它被解释为字符串而不是变量。
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。