Created
February 1, 2015 11:42
-
-
Save kharissulistiyo/fb519dadedc7de38200a to your computer and use it in GitHub Desktop.
Modify WordPress loop to split posts into groups of two
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Query post | |
$args = array( | |
'post_type' => 'post', | |
'posts_per_page' => 10 | |
); | |
$posts = get_posts($args); | |
$count = 0; | |
$div_open = '<div class="every-two" style="background:#f7f7f7; padding: 5px; margin-bottom: 10px;">'; | |
$div_close = '</div><!-- /.every-two -->' . "\n\n"; | |
// Loop begins | |
foreach ($posts as $post) : | |
$count++; | |
$post_split_every = 2; // Split post every group of two | |
$div_close = ($count == 0) ? '' : $div_close . "\n"; | |
if ( 0 == ( $count - 1 ) % $post_split_every ) { | |
echo $div_close. $div_open; | |
} | |
echo '<p>'. $post->post_title . '</p>'; | |
endforeach; wp_reset_postdata(); | |
// Loop ends | |
echo $div_close; // Div close for the last item |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment