簡體   English   中英

顯示來自選定類別的帖子WordPress

[英]Show a post from selected categories wordpress

我正嘗試顯示來自多個類別的一篇文章。 我的代碼僅顯示第一類帖子:\\有什么建議嗎?

<?php

        $args = array(
        'cat' => 1,15,
        'post_type' => 'post',
        'posts_per_page' => '1',
        );

        $query = new WP_Query( $args );

        if ( $query->have_posts() ) :

            while ($query->the_post()):

                the_title();
                the_post_thumbnail(array(200, 200));



            ?>
<?php  endwhile;
            endif;?>

請遵循代碼以了解如何通過傳遞類別ID來顯示所選類別項目中的過帳項目。

$args = array(
        'post_type'         => 'post', // post type
        'posts_per_page'    => -1, // number of post items
        'tax_query'         => array(
            array(
                'taxonomy'  => 'category',
                'field'     => 'term_id',
                'terms'     => array( 16, 244 ) // pass the ID of the post category, separated by a comma.
            )
        )
    );

您定義了'posts_per_page' => '1'因此您正在確切地得到所要的:1個帖子。 來自類別1或15,以最新帖子為准。 如果您想從EACH類別中選1個帖子,我將循環您的代碼,每次使用不同的類別(僅1個)。

唯一的是,那將是您提供的類別ID的順序,而不是按日期排序。 另外,如果您有多個類別的帖子,那么您可能最終兩次發表相同的帖子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM