Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.
    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

share|improve this question
    
Post all of you code please. And don't use query_posts at all. This question has been answered multiple times, by the way. Do some research and edit the question with something more specific. –  s_ha_dum Sep 6 '13 at 13:42
    
@s_ha_dum sorry, I don't have code. If I had the code I would not be here. –  marco Sep 6 '13 at 13:47
1  
Ok. I gave you a place to start. This question has been asked and answered many times in many variations. And second, you have no loop code at all? The question format for the site is "How can I fix this broken code?" not "Can you pretty please write my code for me?" –  s_ha_dum Sep 6 '13 at 13:57
add comment

3 Answers 3

Please don't use query_posts();, use WP_Query class or get_posts(); instead.

To target odd/even posts in your loop:

//I will use WP_Query class instance
$args( 'post_type' => 'recipe', 'posts_per_page' => 5 );

//Set up a counter
$counter = 0;

//Preparing the Loop
$query = new WP_Query( $args );

//In while loop counter increments by one $counter++
if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); $counter++;

    //We are in loop so we can check if counter is odd or even
    if( $counter % 2 == 0 ) : //It's even

        the_title(); //Echo the title of post
        the_content(); //Echo the content of the post

    else: //It's odd

        if( has_post_thumbnail() ) : //If the post has the post thumbnail, show it
            the_post_thumbnail();
        endif;

    endif;

endwhile; wp_reset_postdata(); endif;

Hope it helps!

share|improve this answer
add comment

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...

<?php // Start the loop ?>
<?php while ( have_posts() ) : the_post(); ?>

<?php if ( ( is_archive() || is_author() ) && ( !is_category() && !is_tag() ) ) : // Archives ?>
    <li>
      <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'blaskan' ), the_title_attribute( 'echo=0' ) ); ?>"><?php the_title(); ?></a>
      <time datetime="<?php the_date('c'); ?>"><?php print get_the_date(); ?></time>
    </li>
<?php else: // Else ?>

Modified code that displays the date of publishing, only on even numbered posts in an author's archives...

<?php $posts_count = 1; // Start the loop ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php ++$posts_count; ?>

<?php if ( ( is_archive() || is_author() ) && ( !is_category() && !is_tag() ) ) : // Archives ?>
    <li>
      <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'blaskan' ), the_title_attribute( 'echo=0' ) ); ?>"><?php the_title(); ?></a>
      <?php if($posts_count % 2): ?> <time datetime="<?php the_date('c'); ?>"><?php print get_the_date(); ?></time> <?php endif; ?>
    </li>
<?php else: // Else ?>
share|improve this answer
add comment

You don't need a new variable for counting posts, WordPress has one already in $wp_query->current_post.

<?php while (have_posts()): the_post() ?>
    <?php if ($wp_query->current_post % 2 == 0): ?>
        odd
    <?php else: ?>
        even
    <?php endif ?>
<?php endwhile ?>

If you use a custom WP_Query instance as iEmanuele suggested then it will be $query->current_post instead.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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