Stop duplicate posts

时间:2014-07-04 作者:Adam

我有三个循环,我想阻止他们在每个循环中显示相同的帖子。

任何帮助都将不胜感激

<?php
$i;
$args = array( \'numberposts\' => 8);
$posts= get_posts( $args );
if ($posts) {
    foreach ( $posts as $i => $post )
         {
        setup_postdata($post);

    }
}
?>
<div class="item" id="<?php echo $i; ?>" >
<?php
$a = 0;
foreach( $posts as $i => $post ) :
if ($a == 4) { break; }
?>
            <div class="col-md-6 nopadding view view-ninth" style="">

                            <?php the_post_thumbnail(\'large\'); ?>
                            <div class="content">
                                                    <h2><?php the_title(); ;?></h2>
                            <p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,15) . \'...\'; ?></p>
                            <a href="<?php the_permalink() ?>" class="info">Read More >></a>
                                                    </div>
                            </div>
 <?php $a++; endforeach;  ?>
</div><!-- end item -->


<div class="item" id="<?php echo $i; ?>" >
<?php
$a = 0;
foreach( $posts as $i => $post ) :
if ($a == 4) { break; }
?>
            <div class="col-md-6 nopadding view view-ninth" style="">

                            <?php the_post_thumbnail(\'large\'); ?>
                            <div class="content">
                                                    <h2><?php the_title(); ;?></h2>
                            <p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,15) . \'...\'; ?></p>
                            <a href="<?php the_permalink() ?>" class="info">Read More >></a>
                                                    </div>
                            </div>
 <?php $a++; endforeach;  ?>
</div> 


<div class="item" id="<?php echo $i+1; ?>" >
<?php
$a = 0;
foreach( $posts as $i => $post ) :
if ($a == 4) { break; }
?>
            <div class="col-md-6 nopadding view view-ninth" style="">

                            <?php the_post_thumbnail(\'large\'); ?>
                            <div class="content">
                                                    <h2><?php the_title(); ;?></h2>
                            <p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,15) . \'...\'; ?></p>
                            <a href="<?php the_permalink() ?>" class="info">Read More >></a>
                                                    </div>
                            </div>
 <?php $a++; endforeach;  ?>
</div>

2 个回复
最合适的回答,由SO网友:Adam 整理而成

    <?php $args = array( \'numberposts\' => 8);
    $posts= get_posts( $args );


   $i=0;
   foreach($posts as $key=>$value) {

    if($i==0) { echo "<div class=\\"item\\" id=\\"".$key."\\" >"; } ?>

                <div class="col-md-6 nopadding view view-ninth" style="">

                                <?php echo get_the_post_thumbnail($value->ID, \'large\'); ?>
                                <div class="content">
                                <h2><?php echo $value->post_title; ?></h2>
                                <p><?php echo string_limit_words($value->post_excerpt,15) . \'...\'; ?></p>
                                <a href="<?php echo get_permalink($value->ID); ?>" class="info">Read More >></a>
                                 </div>
                </div>
     <?php $i++; if($i==4) { $i=0; echo "</div><div class=\\"item\\" id=\\"<?php echo $key; ?>\\" >"; } } ?>

     </div>
这是我最后的解决办法。

SO网友:Pieter Goosen

您可以使用WP_Query 创建自定义循环,然后使用offset 参数跳过上一个循环中的帖子。您将从一个查询中运行3个循环,因此重新运行该查询所需做的就是使用rewind_posts() 每次循环后重置循环

下面是一个示例代码,您可以修改它以满足您的确切需要。这只是一个非常基本的循环

<?php
$args = array( \'posts_per_page\' => 4);
$my_query= new WP_Query( $args ); ?>
<ul>
    <?php
    while ($my_query->have_posts()) : $my_query->the_post();
        ?>
        <li><?php the_title(); ?></li>
        <?php
    endwhile; 
    ?>
</ul>
<?php

$my_query->rewind_posts();

?> 
<ul>
    <?php
    $my_query->query(\'posts_per_page=4&offset=4\');
    while ($my_query->have_posts()) : $my_query->the_post();
        ?>
        <li><?php the_title(); ?></li>
        <?php
    endwhile;
    ?>
</ul>
<?php

$my_query->rewind_posts();

?> 
<ul>
    <?php
    $my_query->query(\'posts_per_page=4&offset=8\');
    while ($my_query->have_posts()) : $my_query->the_post();
        ?>
        <li><?php the_title(); ?></li>
        <?php
    endwhile;
    ?>
</ul>
<?php
wp_reset_postdata();
?>

结束

相关推荐