Help Needed: Show posts in different divs? Screenshots included! (4 posts)

  1. paa1605
    Posted 5 years ago #

    Hey guys,

    Im trying to display posts from a certain category on a certain page. The only problem is that they are listed in pairs where both divs are different sizes so i cant use the standard loop. How do i display the posts so that they take this into account?

    here is my current template page code:

    and here is the result of three posts:

    As you can see, at the moment the code is showing each post in both of the different divs.

    This is something very similar to what i want instead:

    Any help would be much appreciated, thanks.

  2. emilekott
    Posted 5 years ago #

    How about something like:

    <?php $left_right = 'left'; ?>
       <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
         <div class="offer-<?php echo $left_right; ?>">
          <!--Your post content -->
          <?php $left_right = ('left'==$left_right) ? 'right' : 'left'; ?>
       <?php endwhile; else: ?>
         <p>Sorry, no posts we're found.</p>
       <?php endif; ?>

    You will of course need to write the css for the classes offer-left and offer-right but this should alternately put posts in left column and right column...

    Hope that helps



  3. paa1605
    Posted 5 years ago #

    Thanks for the quick reply. I've already created the necessary divs with the correct styling so in your above example would it be as simple as replacing 'offer-left' and 'offer-right' with my equivalents?

  4. emilekott
    Posted 5 years ago #

    It should be -

    Code is quite simple - is sets up a variable ($left_right) and each time wordpress goes through the loop, the value of the variable is changed between "left" and "right".

    This value is then appended to offer- to make two classes, offer-left and offer-right (alternately)...

    I hope that makes sense.



Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.