在WordPress中循环显示自定义分类法

4

好的,这可能是一个简单的问题。但出于某些原因,我似乎无法弄清楚。

我有一个名为:Beachevents的自定义文章类型。我有一些活动。 我还有一个名为:Thema的自定义分类法。

在创建我的beachevent页面(不是文章)时,我创建了一些类型的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!';
        }

谢谢!


在解释问题时,如果能够提供相关代码和设置细节,并且表述清晰明了,那么给你点个赞!这样比其他一些问题容易回答多了。 - Tim Malone
2个回答

3

你已经接近成功了!

在你的tax_query中,taxonomy需要指向“beachevents”,terms需要指向“strand-spellen”。

因此,你的代码应该像这样:

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

关于构建查询的更多信息,您可以在WP_Query文档中找到 - 那里有一个关于分类法查询的部分。


谢谢,你的代码起作用了。我确实接近了。还要感谢你在我的问题上给予的+1和评论。现在我只需要按字母顺序排列它们,但那应该不是问题。 - Steggie
请查阅WP_Query文档中的“order”和“orderby”参数。如果答案对您有帮助,请务必接受它 :) - Tim Malone

1

感谢Tim的帮助。这是我为那些遇到同样问题的人准备的完整代码。

<?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!';
            } ?>

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接