Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Wordpress display posts horizontally

Hello,

Does anyone know how To make Wordpress posts display horizontally? I built a theme in bootstrap and I am now trying to configure the loop so it posts across 3 columns.

Thanks!

2 Answers

This is less of a Wordpress issue, and more of a CSS/Bootstrap issue.

A) what version of bootstrap are you using. Assuming it is version 2xx, you use the 'spanX' class to set the col span. usually you have a 12 col grid, so having 3 cols would be "span4" (3X4 = 12). To span all 12 cols that would be "span12". To do this in wordpress, you just need to edit the template file you are looking to change the col spans on, and write the loop to print 3 span4's.inside each row-fluid.

Someone on Stack overflow suggested using this:

$i = 1; echo "<div class='row'>\n"; while( $i <= 10 ){

echo "  <div class='col-lg-4'></div>\n";
if( $i % 3 == 0 ) { echo "</div>\n<div class='row'>\n"; }

$i++;

} echo "</div>\n";

but I don't know how to implement it into my code. I'm new with PHP...

I'm using Bootstrap 3. The code I have now looks like this:

    <div class="row">


    <?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 2) == 0) : $wp_query->next_post(); else : the_post(); ?>

    <div class="col-sm-4">
    <img src="<?php the_field('home_page_slider_image'); ?>" class="img-responsive" >
    <h3><?php the_field( 'description' ); ?></h3>

    </div>

    <?php endif; endwhile; else: ?>
    <div>Alternate content</div>
    <?php endif; ?>

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

    <?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 2) !== 0) : $wp_query->next_post(); else : the_post(); ?>

    <div class="col-sm-4">
    <img src="<?php the_field('home_page_slider_image'); ?>" class="img-responsive" >
    <h3><?php the_field( 'description' ); ?></h3>
    </div>

    <?php endif; endwhile; else: ?>
    <div>Alternate content</div>
    <?php endif; ?>


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

    <?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 2) !== 0) : $wp_query->next_post(); else : the_post(); ?>

    <div class="col-sm-4">
    <img src="<?php the_field('home_page_slider_image'); ?>" class="img-responsive" >
    <h3><?php the_field( 'description' ); ?></h3>
    </div>

    <?php endif; endwhile; else: ?>
    <div>Alternate content</div>
    <?php endif; ?>



    </div>