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 9239237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:56:28+00:00 2026-06-18T07:56:28+00:00

I need to divide the blog into three columns. For that, I wrote: <?php

  • 0

I need to divide the blog into three columns. For that, I wrote:

<?php $i = 0; ?>
<div class="onethird">
<?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 3) == 0) : $wp_query->next_post(); else : the_post(); ?>
    <?php get_template_part( 'content', 'category' ); ?>
        <?php endif; endwhile; endif;  ?>
    </div>

    <?php $i = 0; rewind_posts(); ?>

<div class="onethird">
<?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 3) == 1) : $wp_query->next_post(); else : the_post(); ?>
    <?php get_template_part( 'content', 'category' ); ?>
        <?php endif; endwhile; endif;  ?>
    </div>

<?php $i = 0; rewind_posts(); ?>

<div class="onethird last">
<?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 3) == 2) : $wp_query->next_post(); else : the_post(); ?>
    <?php get_template_part( 'content', 'category' ); ?>
<?php endif; endwhile; endif; ?>
</div>

I’m certain it’s very bad code, but it was the best I could come up with. Then, I noticed the duplicate post problems. Again, after lot of googling, wrong:

<?php $i = 0; $dupe = array(); ?>
<div class="onethird">
<?php if (have_posts()) : while(have_posts()) : $i++; if( (($i % 3) == 0) && (!in_array($post->ID, $dupe)) ) : $wp_query->next_post(); else : the_post(); ?>
<?php $dupe[] = $post->ID; echo $post->ID ?>
    <?php get_template_part( 'content', 'category' ); ?>
<?php endif; endwhile; endif;  ?>
</div>

<?php $i = 0; rewind_posts(); ?>

<div class="onethird">
<?php if (have_posts()) : while(have_posts()) : $i++; if( (($i % 3) == 1) && (!in_array($post->ID, $dupe)) ) : $wp_query->next_post(); else : the_post(); ?>
<?php $dupe[] = $post->ID; echo $post->ID ?>
    <?php get_template_part( 'content', 'category' ); ?>
<?php endif; endwhile; endif;  ?>
</div>

<?php $i = 0; rewind_posts(); ?>

<div class="onethird last">
<?php if (have_posts()) : while(have_posts()) : $i++; if( (($i % 3) == 2) && (!in_array($post->ID, $dupe)) ) : $wp_query->next_post(); else : the_post(); ?>
<?php $dupe[] = $post->ID; echo $post->ID ?>
    <?php get_template_part( 'content', 'category' ); ?>
<?php endif; endwhile; endif; ?>
</div>

Now it just disregards the counter, and the in_array, and display all posts in all three columns.

If anyone has a better solution for displaying posts in three columns, that would be welcome too!

  • 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-18T07:56:30+00:00Added an answer on June 18, 2026 at 7:56 am

    Three loops seem a bit complicated for what you are trying to achieve.

    I would get the total number of posts – wp_count_posts() – and divide it by 3 (round up) to get the number of posts per column.

    Then you only have to loop once and you add </div><div class='onethird'> whenever the remainder of your counter $i divided by the number of posts per column is 0.

    Something like:

    <div class="onethird">
    <?php
    $a_third = ceil(wp_count_posts() / 3);
    $i = 0;
    if (have_posts()) :
      while(have_posts()) :
    
        $i++;
    
        the_post();
    
        if( ($i % $a_third) === 0 ) :
          echo "</div><div class='onethird'>";
        endif;
    
      endwhile;
    endif;
    ?>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of MyObjects which I need to divide into three groups:
I need to divide a C string into tokens. I thought that strtok will
I need to divide tickets into certain columns depending on the amount of tickets
I have a large dataset that I need to divide randomly into 5 almost
I need to divide my calculations into three groups: - which applied to base
Basically, I need a function that will divide n by two and return the
I need to divide screen into two parts. In one part I need to
I need to divide one int into 2 other int's. the first int is
So I am running into an issue with where i need to divide my
There is a PostgreSQL SQL SELECT results. I need to divide rows into quintiles

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.