get_posts is duplicating

时间:2016-02-03 作者:kanarifugl

我有以下代码,其中我想显示一个带有最后一个“post type”post的查询,以及一个我想显示其余内容的查询。问题是最后一个查询与第一个查询重复。如何解决此问题?

    <div class="row">
    <?php $args = array(
        \'posts_per_page\'   => 1,
        \'offset\'           => 1,
        \'category\'         => \'\',
        \'category_name\'    => \'\',
        \'orderby\'          => \'date\',
        \'order\'            => \'DESC\',
        \'include\'          => \'\',
        \'exclude\'          => \'\',
        \'meta_key\'         => \'\',
        \'meta_value\'       => \'\',
        \'post_type\'        => \'prosjekter\',
        \'post_mime_type\'   => \'\',
        \'post_parent\'      => \'\',
        \'author\'       => \'\',
        \'post_status\'      => \'publish\',
        \'suppress_filters\' => true 
    );
    $posts_array = get_posts( $args );
    foreach ($posts_array as $row) {

    ?>
      <div class="col-lg-12">
        <?php the_post_thumbnail() ?>
      </div>
    <?php
    }
    ?>
    </div>
    <div class="row">
    <?php $args2 = array(
        \'posts_per_page\'   => 5,
        \'offset\'           => 2,
        \'tag\' => \'\',
        \'category\'         => \'\',
        \'category_name\'    => \'\',
        \'orderby\'          => \'date\',
        \'order\'            => \'DESC\',
        \'include\'          => \'\',
        \'exclude\'          => \'\',
        \'meta_key\'         => \'\',
        \'meta_value\'       => \'\',
        \'post_type\'        => \'prosjekter\',
        \'post_mime_type\'   => \'\',
        \'post_parent\'      => \'\',
        \'author\'       => \'\',
        \'post_status\'      => \'publish\',
        \'suppress_filters\' => true 
    );
    $posts_array2 = get_posts( $args2 );
    foreach ($posts_array2 as $row2) {

    ?>
      <div class="col-lg-6">
        <?php the_post_thumbnail() ?>
      </div>
    <?php
    }
    ?>

1 个回复
SO网友:Abhik

首先,您需要将第一个循环中要在第二个循环中排除的帖子的ID保存到一个数组中,并使用该数组排除帖子。此外,始终在查询后重置post数据。

这是经过修改的代码。

<div class="row">
<?php $args = array(
    \'posts_per_page\'   => 1,
    \'offset\'           => 1,
    \'category\'         => \'\',
    \'category_name\'    => \'\',
    \'orderby\'          => \'date\',
    \'order\'            => \'DESC\',
    \'include\'          => \'\',
    \'exclude\'          => \'\',
    \'meta_key\'         => \'\',
    \'meta_value\'       => \'\',
    \'post_type\'        => \'prosjekter\',
    \'post_mime_type\'   => \'\',
    \'post_parent\'      => \'\',
    \'author\'       => \'\',
    \'post_status\'      => \'publish\',
    \'suppress_filters\' => true 
);
$posts_array = get_posts( $args );
foreach ($posts_array as $row) {

    $exclude[] = get_the_ID(); // Save The Post ID in an array

?>
  <div class="col-lg-12">
    <?php the_post_thumbnail() ?>
  </div>
<?php
}
wp_reset_postdata();
?>
</div>
<div class="row">
<?php $args2 = array(
    \'posts_per_page\'   => 5,
    \'offset\'           => 2,
    \'tag\' => \'\',
    \'category\'         => \'\',
    \'category_name\'    => \'\',
    \'orderby\'          => \'date\',
    \'order\'            => \'DESC\',
    \'include\'          => \'\',
    \'exclude\'          => \'\',
    \'meta_key\'         => \'\',
    \'meta_value\'       => \'\',
    \'post_type\'        => \'prosjekter\',
    \'post_mime_type\'   => \'\',
    \'post_parent\'      => \'\',
    \'author\'       => \'\',
    \'post_status\'      => \'publish\',
    \'suppress_filters\' => true,
    \'post__not_in\'     => $exclude //Exclude the IDs
);
$posts_array2 = get_posts( $args2 );
foreach ($posts_array2 as $row2) {

?>
  <div class="col-lg-6">
    <?php the_post_thumbnail() ?>
  </div>
<?php
}
wp_reset_postdata();
?>  
EDIT: 由于您只排除最新的帖子,您可以尝试offset 参数也在第二个循环中。

相关推荐