【问题标题】:wordpress query first two posts then query remaining postswordpress 查询前两个帖子,然后查询剩余帖子
【发布时间】:2013-08-19 18:55:37
【问题描述】:

我有一个自定义帖子类型“产品”,我想显示前两个帖子,然后拉入所有剩余帖子以显示在页面的单独部分中。解决此问题的最佳方法是什么?

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    试试这个方法:

    第一个WP_Query 对象为您提供前两个帖子。然后将前两个帖子的 ID 存储在数组中,并将该数组传递给第二个 WP_Query 对象的 post__not_in 参数。

    $args = array(
        'posts_per_page' => 2,
        'post_type' => 'product',
    );
    $query_1 = new WP_Query( $args );
    
    $exclude_posts = array();
    
    while ( $query_1->have_posts() ) {
        $query_1->the_post();
        echo '<li>' . get_the_title() . '</li>';
        $exclude_posts[] = get_the_ID();
    }
    
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'product',
        'post__not_in' => $exclude_posts
    );
    $query_2 = new WP_Query( $args );
    while ( $query_2->have_posts() ) {
        $query_2->the_post();
        echo '<li>' . get_the_title() . '</li>';
        $exclude_posts[] = get_the_ID();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 2013-10-09
      • 2012-09-27
      相关资源
      最近更新 更多