Created
April 12, 2015 22:58
-
-
Save michaelschofield/15693055b193b0c09ad6 to your computer and use it in GitHub Desktop.
Starting Point for the `single-event` template | Adv. WP - Week Four
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 | |
/** | |
* The template for displaying all single events. This specific template | |
* is based of Twentyfifteen single.php. Certain styles and Twentyfifteen-specific functions | |
* may not translate if used with another theme. | |
*/ | |
get_header(); ?> | |
<div id="primary" class="content-area"> | |
<main id="main" class="site-main" role="main"> | |
<?php | |
// Start the loop. | |
if ( have_posts() ) : while ( have_posts() ) : 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 | |
// If comments are open or we have at least one comment, load up the comment template. | |
if ( comments_open() || get_comments_number() ) : | |
comments_template(); | |
endif; | |
// Previous/next post navigation. | |
the_post_navigation( array( | |
'next_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Next', 'twentyfifteen' ) . '</span> ' . | |
'<span class="screen-reader-text">' . __( 'Next post:', 'twentyfifteen' ) . '</span> ' . | |
'<span class="post-title">%title</span>', | |
'prev_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Previous', 'twentyfifteen' ) . '</span> ' . | |
'<span class="screen-reader-text">' . __( 'Previous post:', 'twentyfifteen' ) . '</span> ' . | |
'<span class="post-title">%title</span>', | |
) ); | |
// End the loop. | |
endwhile; endif; | |
?> | |
</main><!-- .site-main --> | |
</div><!-- .content-area --> | |
<?php get_footer(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment