Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display WordPress posts into 3 columns horizontally?

I am integrating my theme built with Bootstrap into WordPress and I am now faced with the challenge of displaying my posts horizontally instead of vertically. The design uses 3 columns.

The solution for two columns posted at this site (http://perishablepress.com/two-column-horizontal-sequence-wordpress-post-order/) was helpful but it posts repeats of previously displayed posts when used with 3 columns.

Here is my code:

    <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 = 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; ?>



    </div>

Any help would be grateful.

Thank you.

like image 826
Darcy Paterson Avatar asked Jan 18 '14 03:01

Darcy Paterson


People also ask

How do I create a 3 column layout in WordPress?

While in the post editor, move your cursor between two blocks on your page, and click the 'Add Block' button. Now choose the 'Columns' block. This will insert a 2 column block, and you can adjust the number of columns via the 'Block Details' area of the dashboard.

How do I change the column layout in WordPress?

Click and drag to select all the desired blocks that you want to turn into columns. Click the block icon near the left side of the block toolbar. Click “Columns” under the list “Transform to.”

How do I change the layout of a WordPress post?

From the All Posts page, hover over the post of which you want to change the layout and click on the Edit option. Next, scroll down, and you'll find the Page Settings option. Thereafter, navigate to General >> Layout. There are various layout options you can choose from.


2 Answers

Take a look on this example, Its working like you want, and arrange your code according to this example.

$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";

http://codepad.org/Qesw28Cw

like image 107
jogesh_pi Avatar answered Sep 27 '22 23:09

jogesh_pi


I built the html strings into a dynamic array, then echo the rows after the have_posts() loop. This divides the number of posts by 4 then orders them vertically in 4 columns. Here is my example:

$query = new WP_Query(array(
                        'post_status'   => 'publish',
                        'orderby'       => 'title',
                        'order'         => 'ASC',
                        'posts_per_page'    => -1
                        ));

$post_count = $query->post_count;
$posts_per_column = ceil($post_count / 4);      

$rows = array();                                            
$count = 0;
while ($query->have_posts())
{ $query->the_post(); 
    if($rows[$count] == ""){ $rows[$count] = '<div class="row">'; }
    $rows[$count] = $rows[$count] . '<div class="col-sm-3">' .
            '<div class="post-title">
             <a href="'.get_permalink().'">'.get_the_title().'</a></div>' .
            '<div class="post-author">by '. get_the_author() .'</div></div>';
    $count++;                           
    if ($count == $posts_per_column ) { $count = 0; }   
}

foreach ($rows as $row) { echo $row . '</div>'; }
like image 29
Bridget Arrington Avatar answered Sep 28 '22 01:09

Bridget Arrington