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 (将#修改为@)