Skip to content

Instantly share code, notes, and snippets.

@wkw
Last active December 28, 2015 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkw/7549730 to your computer and use it in GitHub Desktop.
Save wkw/7549730 to your computer and use it in GitHub Desktop.
Straightforward way to layout Wordpress posts in columns. The example is using Bootstrap styles, but it easily adapts to HTML tables, or any other markup.
/**
* utility to check if more posts available in the loop, avoiding side-effects
* mentioned in the "Note" on this page: http://codex.wordpress.org/Function_Reference/have_posts
* if more are available, it will advance the internal counter by calling the_post()
* -- this function goes in functions.php or (RootsTheme: lib/custom.php)
*/
if( !function_exists('more_posts') ){
function more_posts() {
global $wp_query;
$hasPostsLeft = $wp_query->current_post + 1 < $wp_query->post_count;
if( $hasPostsLeft ){
the_post();
return true;
}
return false;
}
}
// This snippet goes into your WordPress template file
// to do more than two columns, repeat the code for column 2.
// will always properly close the rows. Easily adapted to using HTML tables or other
// formatting
<?php while (have_posts()) : the_post(); ?>
<div class="row">
<div class="col-md-6">
<?php get_template_part('templates/content', get_post_format()); ?>
</div>
<div class="col-md-6">
<?php if( more_posts() ):
get_template_part('templates/content', get_post_format());
endif;
?>
</div>
</div><!-- .row -->
<?php endwhile; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment