0

I wanted to style every second post inside of a query post and I found a solution which nearly works.

the thing is: it outputs just two posts (older ones will be igrnored) but i want to output ALL posts but with every second one with a sepcific class.

Here's what i did so far…

<?php
$paged = (get_query_var('paged')) ?   get_query_var('paged') : 1;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page='.get_option('posts_per_page').'&paged=' . $paged);
                 global $wp_query;
                        query_posts(array_merge($wp_query->query, array(
                            'paged'          => get_query_var('paged'),
                            'posts_per_page' => 9999999
                        )));
                ?>
<?php if ($wp_query->have_posts()) : ?>
<?php $count = 0; ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php $count++; ?>

<?php if ($count == 1) : ?>

<div class="projektitem">
</div><!-- .projektitem --> 

<?php elseif ($count == 2) : ?>      

<div class="projektitem last">
</div><!-- .projektitem --> 

<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?> 
6
  • Just use % 2, not % 3 from the duplicate question’s answer.
    – fuxia
    Dec 12, 2013 at 10:19
  • thanks toscho… can i use it with this post structure? and if, how? (der "multilingual-press-toscho" vom wceu?) Dec 12, 2013 at 10:32
  • i'm sorry, id din't get it in the first place... i just have to use the "post_class"? is it that simple? Dec 12, 2013 at 10:34
  • <div class="projektitem <?php print 0 === ++$wp_query->current_post % 2 ? 'even' : 'odd'; ?>"> should do the trick.
    – fuxia
    Dec 12, 2013 at 10:38
  • thanks.. but now EVERY post gets the class "odd" or in my case "last"?!?! Dec 12, 2013 at 10:49

0

Browse other questions tagged or ask your own question.