Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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>