Skip to content

Instantly share code, notes, and snippets.

@michaelschofield
Created April 8, 2015 06:03
Show Gist options
  • Save michaelschofield/bfcab0ae33bbb8bf9ac3 to your computer and use it in GitHub Desktop.
Save michaelschofield/bfcab0ae33bbb8bf9ac3 to your computer and use it in GitHub Desktop.
Should be in advwp_event_manager/public/templates/
<?php
/**
* Used to display event taxonomy pages.
*/
get_header(); ?>
<section id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
function advwp_edit_event_order( $orderby ) {
return 'mt1.meta_value, mt2.meta_value DESC';
}
// This may be the ugliest line of code ever. It is an unnecessarily complex ternary operator,
// just a different style of writing if {} else {} statements.
$taxonomy = ( is_tax() ? ( is_tax('series') ? 'series' : ( is_tax( 'event_type' ) ? 'event_type' : ( is_tax( 'location' ) ? 'location' : ( is_tax( 'patron_type' ) ? 'patron_type' : null ) ) ) ) : null );
$qobj = get_queried_object();
$args = array(
// fetch only "events""
'post_type' => 'event',
// we can't orderby a meta_value if the
// key isn't present ... - even if we
// aren't actually using 'orderby' directly
// in the query.
'meta_key' => 'advwp_start_date',
// We define multiple keys using 'meta_query'
'meta_query' => array(
// the first key, or mt1
array(
'key' => 'advwp_start_date'
),
// the second key, or mt2
array(
'key' => 'advwp_start_time'
)
),
'nopaging' => true,
$taxonomy => $qobj->slug
);
add_filter( 'posts_orderby', 'advwp_edit_event_order' );
$the_query = new WP_Query( $args );
remove_filter( 'posts_orderby', 'advwp_edit_event_order' );
if ( $the_query->have_posts() ) : ?>
<header class="page-header">
<?php
the_archive_title( '<h1 class="page-title">', '</h1>' );
the_archive_description( '<div class="taxonomy-description">', '</div>' );
?>
</header><!-- .page-header -->
<?php
// Start the Loop.
while ( $the_query->have_posts() ) : $the_query->the_post();
// Once within the loop, we can create fetch information from the post's custom fields
// Some of this is contingent on whether our event is All Day or Multi-Day,
// so we first need to declare variables for these values. In PHP, if you
// want to pass a value between conditions (if / then statements), they need
// to be declared outside of those conditions first. So even though we
// don't know yet if an event is All Day or Multi-Day, we just have to
// establish a starting point. Let's assume that the default is that
// events are neither All Day or Multi-Day.
$allday = false;
$multiday = false;
// The reality as to whether these variables are true or not is
// stored is stored in the checkboxes, which I happened to call
// 'scheduling options'. So let's get that first:
$scheduling_options = get_post_meta( get_the_ID(), 'scheduling_options', true);
// All events have a start day, so let's get that
$start_date = get_post_meta( get_the_ID(), 'advwp_start_date', true );
// The full picture of what is the beginning and end of an event
// isn't just the day - it's the time. To be able to sort and organize events
// by start time, especially when they are in the same day, we need to
// have a value that is both the day of the event and the time of the event.
// We don't know that just yet, but let's create those variables and set
// their default value as just the day of the event - which we already have in
// $start_date.
$start = $start_date;
$end = $start_date;
// Now, based on the value of our $scheduling_options, we will figure out
// whether custom fields exist for start and end time, and set those. This
// begins with the condition that there are NO scheduling_options, meaning
// neither of the boxes were checked - or the default state of the event.
// The !$scheduling_options means "No value for $scheduling_options".
if ( !$scheduling_options ) {
$start_time = get_post_meta( get_the_ID(), 'advwp_start_time', true);
$end_time = get_post_meta( get_the_ID(), 'advwp_end_time', true );
// Now with a $start_time and $end_time, we can update our
// $start and $end variables to include day and time
$start = $start_date . $start_time;
$end = $start_date . $end_time;
} else { // However, if there ARE $scheduling_options
// If the "All Day" box was checked, let's update
// $allday to true
if ( in_array( "allday", $scheduling_options) ) :
$allday = true;
else : // Otherwise, update our $end variable
$end = get_post_meta( get_the_ID(), 'advwp_end_date', true ) . get_post_meta( get_the_ID(), 'advwp_end_time', true);
endif;
// If the "Multi-day" box was checked ...
if ( in_array( "multiday", $scheduling_options) ) :
$multiday = true;
$start = $start_date;
$end = get_post_meta( get_the_ID(), 'advwp_end_date', true);
endif;
}
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
// Post thumbnail.
twentyfifteen_post_thumbnail();
?>
<header class="entry-header">
<?php
if ( is_single() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
endif;
?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php
// To ensure that we are successfully fetching our custom
// fields, we'll 'echo' - or print -- the variables we stored
// above.
echo date( 'l, F jS', strtotime( $start ) ) . ' from ' . date( 'g:i a', strtotime( $start ) ) . ' - ' . date( 'g:i a', strtotime( $end ) ) . '<br>';
the_content();
wp_link_pages( array(
'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
) );
?>
</div><!-- .entry-content -->
<?php
// Author bio.
if ( is_single() && get_the_author_meta( 'description' ) ) :
get_template_part( 'author-bio' );
endif;
?>
<footer class="entry-footer">
</footer><!-- .entry-footer -->
</article><!-- #post-## -->
<?php
// End the loop.
endwhile;
// Previous/next page navigation.
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'twentyfifteen' ),
'next_text' => __( 'Next page', 'twentyfifteen' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
) );
endif;
?>
</main><!-- .site-main -->
</section><!-- .content-area -->
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment