1

(updated)

I´m using this query to loop divs in to my front page slider. I use counters to split 4 posts into a slider, then open a new slider etc. Also a counter to set a class to every fourth post (to fix margin). This worked great until I wanted to use the 'post_per_page' argument. If I put it to -1 or something dividable by 4, I get an infinite loop that loops in all posts ad infinitum.

So. It´s pretty clear that my problem has to do with the counters. I´m not a coder myself and put this code together from different sources and with the help of the almighty internet.

This is what my full query looks like:

<ul id="slider1">
<?php 
$args = array(
'post_type' => 'fastighet',
'posts_per_page' => -1,
'orderby' => 'ASC'
             );
?>
<?php $query = new WP_Query( $args ); ?> 
<?php if( $query->have_posts() ) : ?>
<li class="4slide">     
<?php $count = 0; ?>    
<?php while ($query->have_posts()) : $query->the_post(); $c++;
if( $c == 4) {
$style = 'thumbnail-fourth';
$c = 0;
}
else $style=''; ?> 

<div class="fastighet-thumbnail <?php echo($style) ?>" id="post-<?php the_ID(); ?>">
    <div class="fastighet-thumbnail-image">
        <a href="<?php the_permalink();?>"><?php the_post_thumbnail( 'admin-list-thumb', array( 'alt' => ''.get_the_title().'', 'title' => ''.get_the_title().'' )); ?></a> 
    </div>

    <div class="fastighet-title">
        <a href="<?php the_permalink();?>"><?php the_title(); ?></a>   
    </div>                                  
</div> <!-- end .fastighet-thumbnail -->

<?php $count++; if ( ( $count % 4 ) == 0 && $query->have_posts() ) { ?> 
<!-- if counter equals 4 then close the slide and open a new one -->
</li>
<li class="4slide">
<?php } endwhile; ?>
</li>
<?php endif; ?>     
<?php wp_reset_query(); ?>  
</ul>

Can anybody see what´s wrong with this? How I could modify it to allow me to use 'posts_per_page' like I should be?

2 Answers 2

1

Remove the $posts = get_posts($args) line; not needed if you're calling a WP_Query afterwards.

7
  • Thanks. I still get an infinite loop though :/
    – Johan Dahl
    Commented Mar 14, 2012 at 9:22
  • 1
    Can you post how does the loop continue? do you have a while ( $query->have_posts() ) next?
    – englebip
    Commented Mar 14, 2012 at 9:26
  • Can't comment above, but I think you're mixing $c and $count in your code.
    – englebip
    Commented Mar 14, 2012 at 9:37
  • Hm. I have two counters, a mish mash of code that I put together from different sources. One to put the classes on every forth div, and one to split them into different <li>:s. Maybe I could combine them somehow but I´m not good enough coder to do that. If anyone has any suggestions on how to optimize this code that would be great.
    – Johan Dahl
    Commented Mar 14, 2012 at 9:41
  • @englebip has a good point 'Can you post how does the loop continue?' - if the above code is only part of a template, please paste the full code into a pastebin.com and post the link to it here.
    – Michael
    Commented Mar 14, 2012 at 13:16
1

This is old but i'll add the answer anyway for any future visitors

The problem is that $query->have_posts() also do a cleanup & reset all the posts when there are no posts left to loop. When this function is called inside the main while loop as the if condition, it reset the posts & then when it's called again in while condition, it starts the loop again from the beginning.

To solve this problem, use $query->current_post + 1 != $query->post_count inside your if condition

1
  • useful to know that about have_posts()
    – djb
    Commented Mar 22, 2013 at 18:37

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.