0

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

4

1 回答 1

0

试试这个方法:

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

$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();
}
于 2013-08-19T10:55:37.817 回答