首页 > 解决方案 > 仅显示在 Wordpress 中有帖子的术语

问题描述

我有一个适用于两种分类法的 Wordpress 帖子类型。我想显示分类法“locationcat”中所有术语的列表,该列表使用其他分类法“menucat”中的术语“午餐”。可悲的是,我已经能够打印两种分类法的所有术语。这是我的代码:

$terms = get_terms( array(
'hide_empty' => true,
                        'tax_query' => array(
                         'relation' => 'AND',
                            array(
                                    'taxonomy' => 'locationcat',
                            'hide_empty' => true,
                            ),
                            array(
                                    'taxonomy' => 'menucat',
                                    'field'    => 'slug',
                                    'terms'    => 'lunch',
                                    'hide_empty' => true,
                            ),
                        ),
                ) );
                foreach ($terms as $sc) {
                    $link = $sc->slug;
                    echo '<li><a href="#'. $link .'">'.$sc->name.'</a></li>';
                }

标签: wordpresstaxonomyterm

解决方案


当您询问“仅显示在 Wordpress 中有帖子的条款”时,您的帖子有点令人困惑。我假设您的意思是“使用来自 2 个分类法的术语显示帖子”。

如果这是正确的,那么这就是您的解决方案。

$args = array(
    'post_type' => 'your_post_type',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'tax_1'
            'field'    => 'slug',
            'terms'    => 'term-slug1'
        ),
        array(
            'taxonomy' => 'tax_2',
            'field'    => 'slug',
            'terms'    => 'term-slug1'
        )
    )
);
$posts = get_posts( $args );

然后继续你的循环。

如果我误解了您的问题,请尝试改写它。


推荐阅读