Chris Heinen
Forum Replies Created
Viewing 1 replies (of 1 total)
-
Forum: Themes and Templates
In reply to: Need help with 2 column loopI just finished creating a tutorial on how to add different types of columns in a WordPress template. The first example from my tutorial sounds exactly like what you need.
<?php ????// Custom loop that adds a clearing class to the first item in each row ????$args = array('numberposts' => -1, 'order' => 'ASC' ); //For this example I am setting the arguments to return all posts in reverse chronological order. Odds are this will be a different query for your project ????$allposts = get_posts($args); ????$numCol = 2; // Set number of columns ? ????// Start Counter ????$counter = 0; ????foreach ($allposts as $post) { ????????$content = '<div class="columns-'.$numCol.($counter % $numCol == 0 ? ' first' : '').'">'; // Add class to first column ????????$content .= '<h3><a href="'.$post->guid.'">'.$post->post_title.'</a></h3>'; ????????$content .= $post->post_content; ????????$content .= '</div>'; ????????echo $content; ????????$counter ++; ????} ?> ? <style type="text/css"> ????/* Added styles here for the demo, but ideally you would put this in a stylesheet.*/ ????.columns-2 { ????????float:left; ????????width:45%; ????????margin-left:10%; ????} ????.first { ????????clear:both; ????????margin-left:0; ????} </style>
The above code uses the modulus operator like @alchymyth suggests in order to add a “clearing” class every X posts in a loop. CSS is then used to float the posts into columns and clear at the beginning of each row.
Feel free to check out the full tutorial here: https://heinencreative.com/tutorials/adding-columns-to-a-wordpress-template
Viewing 1 replies (of 1 total)