I am using Bootstrap 3 within Wordpess and have an issue getting my archive posts to display across the page in a grid format. My wordpress loop code is…
<?php if ( have_posts() ) : ?>
<?php
$args=array(
'post_type' => 'artist',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li>
<img src="<?php the_field('artist_photo'); ?>" alt="" class="img-responsive" />
</li>
<?php endwhile;
}
wp_reset_query();
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
This displays a list containing the post’s image. Right now, they list one after the other down the page.
How would I get them to use my bootstrap grid showing 4 across the page, then the next 4 in the row beneath, then the next 4 in row beneath that like this…
<div class="row">
<div class="col-md-3">
<li>image 1 goes here</li>
</div>
<div class="col-md-3">
<li>image 2 goes here</li>
</div>
<div class="col-md-3">
<li>image 3 goes here</li>
</div>
<div class="col-md-3">
<li>image 4 goes here</li>
</div>
</div>
etc. Is that possible? Basically i want the WordPress loop to list ALL of my posts 4 across the page instead of one after the other in a html list down the page.
I just think all of you have complicated the thing very much.The main problem of all of your solutions is that if you do not have the equal number of elements in the row you do not close the final row, and you get a real mess…
Let me explain my solution with only one IF (similar to yours, but without complicated stuff, and adjustable)
And here is the example in the loop (I do not get into which WP_Query you display and how did you get your posts)
Note that you can change the value of
$columns_num = 4
. You can even make a select box from your customizer and to just select how many columns per row you want. Ofcourse, value must divide number 12 (bootstrap columns) without rest. So just 1, 2, 3, 4 and 6 are acceptible.Yes you can do it.
I dont think any of these options fully work. They are assuming that the number of elements/posts is exactly divisible by the column number. For example:
10 posts diveded by 2 = 5 nice closed rows
but
10 posts divided by 3 = 3 nice rows and one row not closed by a final div.
My solution was to check the counter after the loop, if it was divisible ignore, else add the closing div.
THANKS TO GERMAIN THIS COMPLETE CODE ANSWERS MY QUESTION….
These solutions work well, but they aren’t responsive, as in, without adding media calls to override bootstrap’s columns, they will always output the same number of posts per row, regardles of the screen size.
I wanted a 3 column grid display that utilizes bootstrap column widths, floats and clearfixes, which meant that in order to avoid the non-uniform staggering that happens when floated post heights are different, all posts must be the same height.
I was originally trying to do this with bootstrap’s .row-eq-height, but it uses flexbox, which displays all columns on the same row, regardless of the column sizes.
This person’s jQuery solution solved my issue…
Same height column bootstrap 3 row responsive
… and then add the .col-eq-height class to your defined bootstrap column…
Which results in a responsive, 3 column, stackable bootstrap grid of you posts.
Hope that was on topic and helps someone out.