繁体   English   中英

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

[英]Display custom taxonomy in a loop Wordpress

好吧,这可能很简单。 但出于某种原因,我似乎无法弄明白。

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

在制作我的beachevent页面(而不是帖子)时,我创建了一些类型的thema(主题)。 喜欢:Strand Spellen(slug is 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文档很有用 - 那里有关于分类查询的部分。

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

<?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排序。 希望我正确编码...

暂无
暂无

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

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