-
-
Save jeffreyvr/911a627be078fabf6d707651156903d8 to your computer and use it in GitHub Desktop.
Splitting the WordPress loop for alternate grids
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
<div class="grid"> | |
<?php | |
posts_from_loop( 2, function() { | |
get_template_part( 'partials/post-w50' ); | |
}); | |
?> | |
</div> | |
<div class="grid"> | |
<?php | |
posts_from_loop( 3, function() { | |
get_template_part( 'partials/post-w33' ); | |
}); | |
?> | |
</div> | |
<?php while ( have_posts() ) { | |
the_post(); | |
?> | |
<?php get_template_part( 'partials/post-w100' ); ?> | |
<?php } ?> |
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
<?php | |
/** | |
* Fetch a specific amount of posts from the main query. | |
* | |
* @param integer $amount Amount of posts. | |
* @param callback $callback A callback function. | |
* @return void | |
*/ | |
function posts_from_loop( $amount, $callback ) { | |
global $wp_query; | |
while ( ( $count < $amount ) && ( $wp_query->current_post + 1 < $wp_query->post_count ) ) { | |
$wp_query->the_post(); | |
$callback(); | |
$count++; | |
} | |
} |
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
<?php | |
$count = 0; | |
while ( have_posts() ) { | |
the_post(); | |
if ( $count == 0 || $count == 2 ) { | |
echo '<div class="grid">'; | |
} | |
if ( $count <= 1 ) { | |
get_template_part( 'partials/post-w50' ); | |
} elseif ( $count > 1 && $count <= 4 ) { | |
get_template_part( 'partials/post-w33' ); | |
} else { | |
get_template_part( 'partials/post-w100' ); | |
} | |
if ( $count == 1 || $count == 4 ) { | |
echo '</div>'; | |
} | |
$count++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of the grid: