Skip to content

Instantly share code, notes, and snippets.

@renventura
Last active June 10, 2016 23:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save renventura/6e213ad71a68b991dec2 to your computer and use it in GitHub Desktop.
Save renventura/6e213ad71a68b991dec2 to your computer and use it in GitHub Desktop.
Archive template for movie post type
<?php //* Mind this opening PHP tag
/**
* Movie Archive Template
*
* Note that the hooks and genesis_get_image() function used in this file are specific to the Genesis Framework
*
* @author Ren Ventura <EngageWP.com>
* @link http://www.engagewp.com/nested-loops-custom-wordpress-queries
*/
//* Remove Post Info and Meta
remove_action( 'genesis_before_post_content', 'genesis_post_info' );
remove_action( 'genesis_after_post_content', 'genesis_post_meta' );
/**
* Remove the default Genesis loop and run our own
*/
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'rv_movie_loop' );
function rv_movie_loop() {
$term_args = array(
'orderby' => 'slug',
'order' => 'DESC'
);
//* Retrieve every year with a movie post
$terms = get_terms( 'movie_years', $term_args );
$years = array();
//* Loop through each term and assemble an array of term slugs
foreach ( $terms as $term ) {
$years[] = $term->slug;
}
/**
* Loop through the years array and instantiate a new WP_Query with each iteration
*/
foreach ( $years as $year ) {
$year_args = array(
'post_type' => 'movie',
'orderby' => 'name',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'movie_years',
'field' => 'slug',
'terms' => $year,
),
),
);
$loop = null;
$loop = new WP_Query( $year_args );
if ( $loop->have_posts() ) {
$count = 0;
//* New <section> for each year
printf( '<section class="%1$s" id="%2$s">', 'movies clearfix', 'movies-' . $year );
//* Year header (i.e. 2015, 2014, etc.)
echo '<h2 style="text-align: center; font-size: 3em; margin-bottom: 40px;">' . $year . '</h2>';
while ( $loop->have_posts() ) {
$loop->the_post();
//* Get the featured image
$img = genesis_get_image( array(
'format' => 'html',
'size' => 'full',
'context' => 'archive',
'attr' => genesis_parse_attr( 'entry-image' ),
) );
//* Uses a four column grid (standard Genesis/Bootstrap columns)
if ( 0 == $count || 0 == $count % 4 ) {
$classes = 'one-fourth first';
} else $classes = 'one-fourth';
printf( '<article class="%s">', implode( ' ', get_post_class( $classes ) ) );
printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), $img );
echo 'Title: ' . get_the_title() . '<br/>Year: ' . $year . '<br/>';
echo '</article>';
$count++;
}
echo '</section>';
wp_reset_query();
} else echo 'No movies';
}
}
genesis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment