【问题标题】:Dispaly only terms that has posts in Wordpress仅显示在 Wordpress 中有帖子的术语
【发布时间】:2019-07-27 09:24:44
【问题描述】:

我有一个适用于两种分类法的 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>';
                }

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    您的帖子有点令人困惑,因为您问的是“仅显示在 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 );
    

    然后继续你的循环。

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

    【讨论】:

      猜你喜欢
      • 2020-02-14
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多