0

I have three loops I want to stop them from displaying the same posts in each one.

any help would be greatly appreciated

<?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>
6
  • What are you trying to accomplish Jul 4, 2014 at 15:29
  • currently it is doing what i want just have the same posts displaying;
    – Adam
    Jul 4, 2014 at 15:31
  • Its for a carousel - four posts per slide - max three slides
    – Adam
    Jul 4, 2014 at 15:31
  • You should add that info to your question via an edit. Unfortunately I don't have time to sit and code, but you should have a look at WP_Query and the offset parameter. But this is just a rough suggestion. Brain is a bit clogged now Jul 4, 2014 at 15:37
  • Thanks @PieterGoosen I tried that but with no luck. I thought that would fix it but nothing happened.
    – Adam
    Jul 4, 2014 at 15:43

2 Answers 2

0

You can use WP_Query to create your custom loop, and then use the offset parameter to skip the posts in the previous loop. You are going to run 3 loops from one query, so all you need to do to rerun the query is to use rewind_posts() after each loop to reset the loop

Here is an example code which you can modify to suite your exact needs. This is just a very basic loop

<?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();
?>
0
    <?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>

This is my fix in the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.