在循环中显示自定义分类 Wordpress

Display custom taxonomy in a loop Wordpress

好的,这可能很简单。但由于某种原因我似乎无法弄清楚。

我有一个名为:Beachevents 的自定义 post 类型。 我有几个事件。我还有一个名为:Thema 的自定义分类法。

在制作 beachevent 页面(不是 posts)时,我创建了一些类型的 thema(主题)。喜欢:Strand Spellen(鼻涕虫是 strand-spellen)。

现在我想制作一个循环,显示带有缩略图和所有其他内容的唯一 strand-spellen。

有人知道我是怎么做到的吗?

我尝试了一些类似的代码,但没有成功。

$args = array(
            'post_type' => 'beachevents',
            'posts_per_page'=> -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'strand-spellen',
                    'field' => 'slug',
                    'terms' => 'all'
                )
            )
        );
        $products = new WP_Query( $args );
        if( $products->have_posts() ) {
            while( $products->have_posts() ) {
                $products->the_post();
                ?>

                    <div class='content'>
                        <h2><?php the_title(); ?></h2>
                    </div>
                <?php
            }
        }
        else {
            echo 'There seems to be a problem, please try searching again or contact customer support!';
        }

谢谢!

你很接近!

在你的tax_query中,taxonomy需要参考'beachevents',terms需要参考'strand-spellen'。

因此,您的代码将如下所示:

    'tax_query' => array(
            array(
                'taxonomy' => 'thema',
                'field' => 'slug',
                'terms' => 'strand-spellen'
            )
        )

有关构建查询的更多信息,您可能会发现 WP_Query documentation 很有用 - 那里有一个关于分类查询的部分。

感谢蒂姆的帮助。这是我为遇到同样问题的人提供的完整代码。

<?php $args = array(
    'post_type' => 'beachevents',
    'posts_per_page'=> -1,
    'orderby' => 'title',
    'order' => 'ASC',
    'tax_query' => array(
        array(
        'taxonomy' => 'thema',
        'field' => 'slug',
        'terms' => 'strand-spellen'
                )
            )
        );

        $products = new WP_Query( $args );
            if( $products->have_posts() ) {
                while( $products->have_posts() ) {
                    $products->the_post();
?>

<div class='content'>
<h2><?php the_title(); ?></h2>
</div>
<?php
    }
        }
            else {
                echo 'There seems to be a problem, please try searching again or contact customer support!';
            } ?>

包括按标题和 ASC 排序。希望我编码正确...