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

时间:2016-04-26 07:45:06

标签: php wordpress wordpress-theming

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

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

在制作我的beachevent页面(而不是帖子)时,我创建了一些类型的thema(主题)。喜欢:Strand Spellen(slu is is strand)。

现在我想创建一个显示唯一带有缩略图和所有内容的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!';
        }

谢谢!

2 个答案:

答案 0 :(得分:2)

你关闭了!

在您的tax_query中,taxonomy需要参考&#39; beachevents&#39;并且terms需要引用&#39; strand-spellen&#39;。

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

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

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

答案 1 :(得分:1)

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

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