Skip to content

Instantly share code, notes, and snippets.

@bob-moore
Created May 26, 2016 15:26
Show Gist options
  • Save bob-moore/64607cdd54dddbc01188042eb99a99d4 to your computer and use it in GitHub Desktop.
Save bob-moore/64607cdd54dddbc01188042eb99a99d4 to your computer and use it in GitHub Desktop.
Wordpress loop for bands post type
<main id="main" class="site-main" role="main">
<?php
// WP_Query arguments
$args = array (
'post_type' => array( 'band' ),
'post_status' => array( 'publish' ),
'nopaging' => true,
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' => 'menu_order',
'cache_results' => true,
'update_post_meta_cache' => true,
);
$loop_count = 1;
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="wrap-everything">
<?php if( $loop_count === 1 ) : ?>
<!-- Do stuff that needs done on very first loop only, like wrapping the top row -->
<?php endif; ?>
<?php if( $loop_count <= 3 ) :?>
<!-- Do stuff for first three bands -->
<?php endif; ?>
<?php if( $loop_count === 3 ) : ?>
<!-- Do stuff that only needs done on third loop, like closing the top row and wrapping the lower rows -->
<?php endif; ?>
<?php if( $loop_count > 3 ) : ?>
<!-- Do stuff for other rows -->
<?php endif; ?>
<?php if( $loop_count === $query->found_posts ) : ?>
<!-- Do stuff that only needs done on last loop, like closing bottom row(s) wrapper -->
<?php endif; ?>
<!-- Increment loop counter -->
<?php ++$loop_count; ?>
<!-- End our loop -->
</div>
<?php endwhile; endif; ?>
<!-- Reset post data -->
<?php wp_reset_postdata(); ?>
</main><!-- #main -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment