how to get a different html for odd/even posts?

    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

Read More
query_posts('category_name=category-name');

then i don’t know how to continue

Related posts

3 comments

  1. 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): ?>
            even
        <?php else: ?>
            odd
        <?php endif ?>
    <?php endwhile ?>
    

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

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

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

Comments are closed.