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

  • Home
  • SEARCH
  • 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 8598737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:13:33+00:00 2026-06-12T01:13:33+00:00

I am trying to adapt the WordPress loop to style posts differently in rows

  • 0

I am trying to adapt the WordPress loop to style posts differently in rows that get
infinitely smaller until all posts are displayed

The concept here is to display posts in rows

first row 1 post
second row 2 posts
third row 3 posts
fourth row 4 posts
fifth row 5 posts
sixth row 6 posts
seventh row 7 posts

and onward until all posts have been retrieved

the below code is limited and does not do the above how would you adapt to make the below do the above?

the below code is functional and can be seen here: http://ccs.btcny.net/redhook/

<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
<div class="style-1"><?php the_content(); ?></div>
<?php elseif ($count == 2 || $count == 3) : ?>
<div class="style-2"><?php the_content(); ?></div>
<?php elseif ($count == 4 || $count == 5 || $count == 6) : ?>
<div class="style-3"><?php the_content(); ?></div>
<?php elseif ($count == 7 || $count == 8 || $count == 9 || $count == 10) : ?>
<div class="style-4"><?php the_content(); ?></div>
<?php elseif ($count == 11 || $count == 12 || $count == 13 || $count == 14 || $count == 15 ) : ?>
<div class="style-5"><?php the_content(); ?></div>
<?php elseif ($count >= 16 ) : ?>
<div class="style-6"><?php the_content(); ?></div>
<?php endif; ?>
<?php endwhile; ?>
  • 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-12T01:13:34+00:00Added an answer on June 12, 2026 at 1:13 am

    Here is my solution. Basically I am setting the row number by checking if the next element is inside the current row. To do that I need to see if next index is equal to the first index of the next row.

    The tricky part is to find the index of the first item in the row. As this is not linear but geometrical order the items number in each row will be increasing. To calculate the first item of the next row I need to add the number of elements in the current row (in this case simply the current row) to the first index of the current row. When I get the first index of the next row and when the next item will be equal to that number I need to go to the next row and set the first index for the current row for that calculated index.

    <?php //The first index of the first row is 1?>
    <?php $row = 1;?>
    <?php $curFirstIndex = 1;?>
    
    <?php //start counting items ?>
    <?php $count = 0;?>
    <?php while (have_posts()) : the_post();?>
    
       <?php //create actual html code ?>
       <div class="style-<?php echo $row;?>"><?php the_content(); ?></div>
    
       <?php //check if the next item is equal to the first index of the next row ?>
       <?php if ($count+1 >= ($row + $curFirstIndex)) :
           <?php 
             //the next item will be in the new row
             //in $curFirstIndex store the first number of the current row
             $curFirstIndex = $row + $curFirstIndex;
    
             //go to the next row
             $row++;
           ?>
       <?php endif; ?>
       <?php $count++;?>
    
    <?php endwhile;?>
    

    [EDIT]

    The above solution works but it has one little flaw, namely it incorporates the logic into the template. WordPress does not follow some strict layer separation but in my opinion it is always good to try to do that if it is possible. So I come up with two ways that can reduce the logic of this in the template file and move the logic outside. Since wordpress has functions.php in its theme we can use this file to create a function which will handle that logic. Here is the code

    Template view file:

    <?php //Initial values?>
    <?php $row = 1;?>
    <?php $count = 1;?>
    <?php while (have_posts()) : the_post();?>
    
       <?php //create actual html code ?>
       <div class="style-<?php echo $row;?>"><?php the_content(); ?></div>
    
       <?php //check if the current item is the last in the row ?>
       <?php if ($count == getRowLastItemIndex($row)) {$row++;} ?>
       <?php $count++;?>
    
    <?php endwhile;?>
    

    in functions.php:

    function getRowLastItemIndex($row) {
        $index = 0;
        for($i = 1; $i <= $row; $i++) {
            $index += $i;
        }
        return $index;
    }
    

    Or make it even more separate in the following way

    The template file

    <?php //Initial values?>
    <?php $row = 1;?>
    <?php $count = 1;?>
    <?php while (have_posts()) : the_post();?>
    
       <?php //create actual html code ?>
       <div class="style-<?php echo $row;?>"><?php the_content(); ?></div>
    
       <?php $row = getRowNumber($row,$count);?>
       <?php $count++;?>
    
    <?php endwhile;?>
    

    And add another function to the functions.php

    function getRowNumber($row,$count) {
    
        if ($count == getRowLastItemIndex($row)) 
            $row++;
        } 
        return $row;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to adapt an xla add-in that I wrote for Excel 2007 to
I'm trying to adapt a VBscript that runs the QWINSTA command against a text
I have a custom accordion script that I'm trying to adapt for another site.
I am trying to adapt the underlying structure of plotting code (matplotlib) that is
I have been trying to adapt my code (a webservice) to get the SQLServerMessages:
I'm trying to adapt the answers for filling a ListBoxFor that has preselected values
Im trying to adapt some code that I have found online that uses this
I am trying to adapt my simple socket server so that it can have
Here's a piece of Oracle code I'm trying to adapt. I've abbreviated all the
I am trying to adapt the AccountController class so that it uses my own

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.