• Hi,
    I read this discussion: “Alternating posts” but I haven’t solve my problem.

    I’d like to print out the posts with the alternative code like:

    <div>Title</div><div>Content</div>
    
    <div>Content</div><div>Title</div>
    
    <div>Title</div><div>Content</div>
    
    <div>Content</div><div>Title</div>
    ...

    Then work with css. . .
    Any ideas? Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • this is only one of many possible ways:
    introduce a counter variable before the loop; then check if it is odd or even (if the division by 2 does not leave a remainder – it is even); increment it before the end of the loop:

    <?php $c = 0 ; ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <?php if( $c % 2 == 0 ) : ?>
    <div><?php the_title(); ?></div><div><?php the_content(); ?></div>
    <?php else : ?>
    <div><?php the_content(); ?></div><div><?php the_title(); ?></div>
    <?php endif; $c++; ?>
    
    <?php endwhile; ?>
    <?php else : endif; ?>

    Edit: It would appear I was too slow!

    I don’t think that the article you linked to is what you’re after as it would only work if post numbers are sequential.

    I’m sure that there’s a plugin for this, but if not, the route I’d take is to start a counter outside “the loop”, on each iteration of the loop you check the status of the counter using the php modulus operator as seen in that article, then increment the counter by 1.

    Thread Starter conair77

    (@conair77)

    I solved!
    Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Alternating view of posts’ is closed to new replies.