Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8482123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:44:37+00:00 2026-06-10T19:44:37+00:00

Basically I need a custom WordPress post loop which outputs posts in the following

  • 0

Basically I need a custom WordPress post loop which outputs posts in the following format (it should be formatted that way for every two posts, so two posts each in a “col” div both contained in the “row” div.):

<div class="row cols2">
<div class="col left">
<a href="#"><img src="featured-image.jpg" /></a>
<h2><a href="#">Blog Post Title</a></h2>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
</div><!-- /.col left -->

<div class="col right">
<a href="#"><img src="featured-image.jpg" /></a>
<h2><a href="#">Blog Post Title</a></h2>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
</div><!-- /.col right -->
</div><!-- /.row cols2 -->

I have more or less achieved this using the following loop:

<?php $counter=1;$grids=2;global $query_string;query_posts($query_string.'&caller_get_posts=1&posts_per_page=6');if(have_posts()):while(have_posts()):the_post();if($counter==1):?>
<div class="row cols2">
<div class="col left">
<a href="<?php the_permalink()?>"><?php the_post_thumbnail('default-thumb')?></a>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt() ?>
</div><!-- /.col left -->

<?php elseif($counter==$grids):?>
<div class="col right">
<a href="<?php the_permalink()?>"><?php the_post_thumbnail('default-thumb')?></a>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt() ?>
</div><!-- /.col right -->
</div><!-- /.row cols2 -->
<?php $counter=0;endif;$counter++;endwhile;endif;?>

The only problem with this is when there is only one post in the “.row cols2” div it messes up because the “.row cols2” closing tag is only outputted when there are two posts in a row. If anyone has any idea on how to help it would be GREATLY appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:44:39+00:00Added an answer on June 10, 2026 at 7:44 pm

    I’m so glad i dont have to work with your code, it’s a nightmare to read. I would recommend not bunching up conditions and loops like that, it just makes reading and debugging so much more difficult.

    On to your question, the basic idea is that you output the container every even count increment, then check the counter at the end to find if your missing any requirements (i.e. table cells in a table, an ending tag for an element e.c.t.).

    So all you need to do is check it at the end (i’ve also rewritten your post output, never duplicate the same code unnecessarily, it could cause you issues later on when you make changes to one and not the other).

    <?php 
    $counter=1;
    $grids=2;
    global $query_string;
    query_posts($query_string.'&caller_get_posts=1&posts_per_page=6');
    
    if(have_posts()):
    
      while(have_posts()):
    
        the_post();
    
        //-- ouput start elements
        if($counter==1): ?>
          <div class="row cols2">
          <div class="col left">
        <?php
        elseif($counter == $grids): ?>
          <div class="col right">
        <?php endif; ?>
    
        <?php /* Output column box content */ ?>
    
        <a href="<?php the_permalink()?>"><?php the_post_thumbnail('default-thumb')?></a>
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <?php the_excerpt() ?>
    
    
    
         <?php /* always need to close the inner div */ ?>
         </div>
    
         <?php 
         //-- if its the second column, then end row and reset counter
        if($counter == $grids): ?>
          </div>
        <?php 
          $counter = 0;
        endif; ?>
    
    
    <?php 
    $counter++;
    endwhile;
    
    if($counter == 1):
      //--- output empty second column div then end the row
      ?>
      <div class="col right"></div>
      </div>
      <?php
    endif; 
        ?>
    
    <?php
    /** Forgot this one */
    endif;
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

basically I need an image button, specifically a custom object that: 1) calls a
Basically I have a custom post type setup called Parts with over 5,000 posts
I basically need a custom function to be used only when, for example, the
Basically need to generate custom(some different then yes no) messeges(alert) in JS , how
basically need to change the value that - admin_url() returns any idea?
I basically need to check if there is an easier way to do this
I'm struggling to do something which I thought should be straight forward, basically I
I have setup a custom Post Type in WordPress and have a question. How
I am writing a PHP function that will need to loop over an array
I need to loop through a list of fields in a custom validator to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.