drpp5680
2014-08-31 20:57
浏览 18
已采纳

在wordpress的一个页面中查询两种不同类型的帖子?

I have two different post types one is books and the other is supports. I am trying to call this two post in one page.

How can I do that? Here is the code I am using to query a post:

<?php query_posts('post_type=books&post_status=publish&posts_per_page=-1&paged='.
get_query_var('paged')); ?>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsh7623 2014-09-01 04:40
    已采纳

    query_posts should never be used. Rather use WP_Query

    $args = array(
               'post_type'=>array('books','supports'),
               'post_status'=>'publish',
               'posts_per_page'=>-1,
               'paged'=>get_query_var('paged')
    );
    
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
        }
        echo '</ul>';
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
    打赏 评论
  • duangang1991 2014-08-31 21:07
    <?php
    query_posts(
        array(
            'post_type'=>array('books','supports'),
            'post_status'=>'publish',
            'posts_per_page'=>-1,
            'paged'=>get_query_var('paged')
            )
        );
    ?>
    
    打赏 评论

相关推荐 更多相似问题