# How do I rewrite this loop as a new WP_Query style-loop?

How do I rewrite the if (have_posts()) : ... while (have_posts()) : the_post(); style Wordpress loop below as a new_query style-loop using <?php $the_query = new WP_Query();? ( As shown in http://codex.wordpress.org/Class_Reference/WP_Query ) The current loop below alternates post classes to display a grid of posts two posts wide (see image below). But I need to rewrite it using a new query so I can: 1) add a post display count, and 2) a post count offset to it. The reason is I need to use it in the page template with another loop based on new query. This loop alternates the class between "uneven post" and "post" to display the posts (see in the image below.) I need to retain that function in a new WP_Query loop. <?php if (have_posts()) :$odd = false;
while (have_posts()) : the_post();
$odd = !$odd;
?>

<div class="<?php if ($odd) echo 'uneven '; ?>post"> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <?php the_excerpt(); ?> </div> <?php endwhile; else: endif; ?>  The loop above displays posts like this: This is a standard new WP_Query loop: <?php$my_query = new WP_Query('offset=5&showposts=10'); ?>

<?php while ($my_query->have_posts()) :$my_query->the_post(); ?>

<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">

<?php the_title(); ?></a>


You're 95% of the way there already. Just add if ( $my_query->have_posts() ) and incorporate your $odd variable:

<?php
$my_query = new WP_Query('offset=5&showposts=10'); if ($my_query->have_posts() ) :

$odd = false; while ($my_query->have_posts()) : $my_query->the_post();$odd = !$odd; ?> <div class="<?php if ($odd) echo 'uneven '; ?>post">

<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

<?php the_excerpt(); ?>

</div>

<?php

endwhile;

endif;

• Ah, that works great. I should have seen that. Thanks! – markratledge Oct 28 '12 at 16:38
• Actually, this doesn't work. ALl the divs have the class .post and don't alternate with the class .uneven. It appears that the loop works from the browser, but the source shows that the classes don't alternate. – markratledge Oct 28 '12 at 22:24
• That actually would be a different sort of question entirely. I was assuming that the PHP/conditional code was working properly, and you merely needed to incorporate it into a custom query loop. – Chip Bennett Oct 29 '12 at 2:34
• Thanks, it looked like it was working, but the lack of the alternating class would be a problem. Actually, I've kind of given up on this, as what I am really working on and need to figure out is a loop like this: a certain number of full posts and then a certain number of excerpts with the odd/even layout for a grid, and which will also support pagination. And the second to ~ pages will be all excerpts in a grid. Just like the Thesis theme does, but I can't figure out how to extract that loop from that theme; and it's proprietary, too. – markratledge Oct 29 '12 at 3:21
• If you would either start a new question with these details, or edit this one with the same, I'll be happy to try to work it through - It will probably be mostly a PHP question, but there are a couple of ways to incorporate it into WordPress. – Chip Bennett Oct 29 '12 at 4:09