I’m trying to create the following front-end using a PHP loop and Twitter Bootstrap’s 12 column grid system:
The HTML output is:
<div class="row">
<div class="col-lg-4">
Content...
</div>
<div class="col-lg-4">
Content...
</div>
<div class="col-lg-4">
Content...
</div>
</div>
<div class="row">
<div class="col-lg-4">
Content...
</div>
<div class="col-lg-4">
Content...
</div>
<div class="col-lg-4">
Content...
</div>
</div>
<div class="row">
<div class="col-lg-6">
Content...
</div>
<div class="col-lg-6">
Content...
</div>
</div>
In PHP (WordPress) I’m wrapping every 3 items in a .row
div:
<?php $i=0; // counter ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ($i%3==0) { // if counter is multiple of 3 ?>
<div class="row">
<?php } ?>
<div class="col-md-4">
Content...
</div>
<?php $i++; ?>
<?php if($i%3==0) { // if counter is multiple of 3 ?>
</div>
<?php } ?>
<?php endwhile; ?>
<?php if($i%3!=0) { // put closing div if loop is not exactly a multiple of 3 ?>
</div>
<?php } ?>
The Problem:
I don’t know how to add the appropriate column number to the items in the last row so that they fill the 12 column grid.
For example, in my illustration above each item in the last row has col-6
(expands 6 columns) filling the 12 grid system. As another example, if there was 1 item in the last row it should have col-12
.
Note: each row has 3 items at most as shown in the illustration and in PHP.
I know the following:
-
Total number of items
$loop->post_count
-
Item number
$i
-
Number of remainder items in the last row
$loop->post_count%3
(I think) -
Total number of columns
12
(12 could be divided by the number of remainder items to figure out the column number to give them)
Question:
How can I use that data in the PHP above to change the column number of the items in the last row so that they will fill the 12 grid (making them them centered)?
7
Answers
I think I found the solution by first finding at which item the last row starts and applying the appropriate column number to all the items in that row:
The advantage is that any number (evenly divisible by 12) can be added to
$max_columns
and it will apply the proper columns.Why don’t you evaluate your modulo?
Print a row at a time determining html class for each element depending on how full the row is; for 0 col-md-4, for 1 col-md-12… You would need some helper structures. Finally print the final row if there’s something in the buffer.
I liked your question because I’m working on a very similar situation. Since other answers are a bit longer, I decided to leave mine here for your consideration. For me, the less variables you use, the best the solution is.
BootstrapContentArranger.php
Test Cases
Note: you can remove the
PHP_EOL
, I used it to read the source better.Everytime I need to to this, I just use
array_chunk
to build a proper array chunk for my rows and columns.For example You have:
$posts = [['id' => 1], ['id' => 2] ...]
Instead of looping and calculating whether to add row, make chunks of your posts:
For someone who don’t want the elements to span all the columns but just a simple grid…. Here’s how to do it in .
The result