query posts
if posts exist
then begin the loop
if post is even: <h1>title</h1><p>content</p>
if post is odd: <div>its image</div>
this is what i’m trying to get, a different output for odd/even posts: for even posts we will show the title and the content while for odd posts we will show its image (the thumbnail, for example).
How to get this result?
I query post in this way
query_posts('category_name=category-name');
then i don’t know how to continue
You don’t need a new variable for counting posts, WordPress has one already in
$wp_query->current_post
.If you use a custom
WP_Query
instance as iEmanuele suggested then it will be$query->current_post
instead.Please don’t use query_posts();, use
WP_Query
class orget_posts();
instead.To target odd/even posts in your loop:
Hope it helps!
You can have a new variable to count the number of posts, then increase it inside the while loop and then check if it is odd or even. Here’s a sample code from Blaskan theme‘s loop.php file that displays an author’s archives…
Modified code that displays the date of publishing, only on even numbered posts in an author’s archives…