簡體   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