简体   繁体   English

我找不到一种方法来使用Wordpress遍历类别和每个类别的帖子

[英]I can't find a way to loop through categories and the posts per categories with wordpress

i'm making a wordpress website with a theme that allowes me to use twig templates per page. 我正在制作一个主题为wordpress的网站,允许我每页使用树枝模板。

In functions.php i get the custom categories i want by doing 在functions.php中,我通过执行操作获得了所需的自定义类别

    $context['test'] = get_terms('workshop_category');

    foreach($context['test'] as $key => $value){
        $args = [
            'post_type' => 'workshop',
            'workshop_category' => $context['test'][$key]->id,
            'posts_per_page' => -1,
        ];

        $context['workshops'] = Timber::get_posts($args);


    }

Within that loop i want to loop all the workshops per workshop_category. 在该循环中,我希望循环每个workshop_category的所有研讨会。 With the current code and the twig code i get all the workshops underneath every category. 使用当前代码和细枝代码,我将所有类别的工作坊都分类了。 But it only needs to be the ones that have that category 但这只需要属于该类别的

<li class="navigation__list__item hasMenu"><a href="#">Workshops</a>
                <div class="dropdown">
                    <ul class="dropdown__menu">
                        {% for workshop in test %}
                            <li class="dropdown__menu__item">
                                <a class="dropdown__menu__item__title" href="{{ workshop.link }}">{{ workshop.name }}</a>
                            </li>
                            {% for w in workshops %}
                                <li class="dropdown__menu__item">
                                    <a class="dropdown__menu__item__title" href="{{ w.link }}">{{ w.title }}</a>
                                </li>
                            {% endfor %}
                        {% endfor %}
                    </ul>
                </div>
            </li>

This is what i get returned: Category 1: workshop 1 workshop 2, Category 2: workshop 1 workshop 2. I 这就是我得到的回报:第1类:车间1车间2,第2类:车间1车间2。

Assuming your workshop_category is a custom post meta you habe to use meta_query key in your $args like this: 假设您的workshop_category是自定义发布元,那么您可以在$args使用meta_query键,如下所示:

$args = [
    'post_type' => 'workshop',
    'meta_query' => [
        ['key' => 'workshop_category', 'value' => $context['test'][$key]->id]
    ],
    'posts_per_page' => -1,
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM