- <?php /**
- * Custom Post Type ftevent (Events) loop template
- * IMPORTANT NOTE: WORDPRESS 3.1 READY. USES:
- * - meta_query (instead of meta_key/value/compare)
- * - tax_query (instead of Multiple Taxonomy Query plugin)
- *
- */
- ?>
- <?php
- /*
- ******************************************************************
- // !SET TAXONOMY TERMS IF FORM POSTED
- ******************************************************************
- */
- // FUNCTIONS USED IN THIS SCRIPT
- // List taxonomy terms as an option list and recall options chosen if applicable
- function osu_list_terms($osu_tax, $osu_recall) {
- // list terms in taxonomy term
- $tax_terms = get_terms($osu_tax);
- // if $osu_recall is true, recall selected options
- foreach ($tax_terms as $tax_term) {
- echo '<option';
- // Check if option chosen is the current <option> being created
- if ($osu_recall == $tax_term->name) {
- echo ' selected="yes"';
- }
- echo '>' . $tax_term->name;
- echo '</option>';
- }
- }
- // Function to echo out date range for Event Custom Post types - Needs CF start
- // and end dates passed to it and relies on osu_date_format() function.
- function osu_showdates($osu_start_d, $osu_end_d) {
- // Reverse the StartEventDate and EndEventDate Custom fields so they display in correct order (dd/mm/yyyy not yyyy/mm/dd)
- $osu_start_d_rev = osu_date_format($osu_start_d);
- $osu_end_d_rev = osu_date_format($osu_end_d);
- // No need to display reversed EndEventDate date if event happens on one day only
- if($osu_end_d !== $osu_start_d || $osu_end_d == '') {
- $osu_end_d_rev = ' - ' . $osu_end_d_rev;
- }
- // If no start date at all, display error message
- if(!$osu_start_d) {
- echo '<span>Event date not available!</span>';
- } else {
- echo $osu_start_d_rev . $osu_end_d_rev;
- }
- }
- // Function to convert spaces to hyphens (turn string into slug)
- function osu_convert_spaces($convert) {
- $converted = preg_replace("/\s/","-",strtolower($convert));
- return $converted;
- }
- // Get post data from filter
- if(isset($_POST["fttype"])) {
- $ft_t = $_POST['fttype'];
- } else {
- $ft_t = '';
- }
- if(isset($_POST["ftperiod"])) {
- $ft_p = $_POST['ftperiod'];
- } else {
- $ft_p = '';
- }
- if(isset($_POST["ftduration"])) {
- $ft_d = $_POST['ftduration'];
- } else {
- $ft_d = '';
- }
- // If 'All' chosen in options, make option blank so all taxonomy terms returned
- if($ft_t == 'All')
- { $ft_t = ''; }
- if($ft_p == 'All')
- { $ft_p = ''; }
- if($ft_d == 'All')
- { $ft_d = ''; }
- ?>
- <?php
- /*
- ******************************************************************
- // !SET THE QUERY
- ******************************************************************
- */
- // QUESTION: Paged results - for some reason pagination doesn't appear
- // on the results page when the filter form is submitted??
- $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
- // Because pagination isn't working once the form is submitted, we'll show all posts (uses hidden input)
- if($_POST['showall']) {
- $ft_ppp = 100;
- } else {
- $ft_ppp = 3;
- }
- // Set todays date to check against the custom field StartEventDate
- $todaysDate = date('Y/m/d');
- // Convert spaces in taxonomies and terms into hyphens so that search works correctly (uses slug)
- $ft_t_ns = osu_convert_spaces($ft_t);
- $ft_p_ns = osu_convert_spaces($ft_p);
- $ft_d_ns = osu_convert_spaces($ft_d);
- // Build query - WP 3.1 only!
- // READ MORE ON 'MULTIPLE TAXONOMY HANDLING' HERE:
- // http://codex.wordpress.org/Function_Reference/query_posts#Taxonomy_Parameters
- $ft_args = array(
- 'post_type' => 'ftevent',
- 'posts_per_page' => $ft_ppp,
- 'paged' => $paged,
- // 'meta_key' is needed to 'orderby' posts by Custom Field StartEventDate
- 'meta_key' => 'StartEventDate',
- 'orderby' => 'meta_value',
- 'order' => 'ASC',
- // Use meta_query to filter results
- 'meta_query' => array(
- array(
- 'key' => 'StartEventDate',
- 'value' => $todaysDate,
- // Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL',
- // 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'.
- 'type' => 'DATE',
- // Operator to test. Possible values are 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
- // We choose 'BETWEEN' because we need to know the date has not passed to show the event
- 'compare' => '>='
- )
- ),
- 'tax_query' => array(
- 'relation' => 'AND',
- array(
- 'taxonomy' => 'fttype',
- 'field' => 'slug',
- 'terms' => array($ft_t_ns),
- // Operator to test. Possible values are 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
- // We choose 'IN' because we need to make sure the term is in the current array of posts
- 'operator' => 'LIKE',
- ),
- array(
- 'taxonomy' => 'ftperiod',
- 'field' => 'slug',
- 'terms' => array($ft_p_ns),
- 'operator' => 'LIKE',
- ),
- array(
- 'taxonomy' => 'ftduration',
- 'field' => 'slug',
- 'terms' => array($ft_d_ns),
- 'operator' => 'LIKE',
- ),
- )
- );
- // Create query
- query_posts($ft_args);
- ?>
- <?php
- /*
- ******************************************************************
- // !FILTER FORM
- ******************************************************************
- */
- ?>
- <?php
- // Check values in array
- print '<p><pre>';
- print_r( $ft_args );
- print '</pre></p>';
- ?>
- <form action="/ftevent/" method="post" id="ftfilter">
- <fieldset>
- <legend>Filter</legend>
- <ul>
- <li>
- <label>Type</label>
- <select name="fttype" id="fttype-select"><option>All</option>
- <?php osu_list_terms('fttype', $ft_t); ?>
- </select>
- </li>
- <li>
- <label>Period</label>
- <select name="ftperiod" id="ftperiod-select"><option>All</option>
- <?php osu_list_terms('ftperiod', $ft_p); ?>
- </select>
- </li>
- <li>
- <label>Duration</label>
- <select name="ftduration" id="ftduration-select"><option>All</option>
- <?php osu_list_terms('ftduration', $ft_d); ?>
- </select>
- </li>
- <li class="end">
- <input type="submit" id="filtersubmit" value="Submit" />
- <input type="hidden" id="showall" name="showall" value="true" />
- </li>
- </ul>
- </fieldset>
- </form>
- <?php
- /*
- ******************************************************************
- // !LOOP STARTS
- ******************************************************************
- */
- ?>
- <?php if ( have_posts() ) : ?>
- <?php while ( have_posts() ) : the_post(); ?>
- <div class="event-thumbnail">
- <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten-child' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php
- if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) {
- the_post_thumbnail('thumbnail');
- } else { ?>
- <img src="<?php echo bloginfo('stylesheet_directory'); ?>/images/thumb-default.jpg" alt="thumb-default" width="65" height="65" />
- <?php } ?>
- </a>
- </div> <!-- End div.event-thumbnail -->
- <div class="taxonomy-terms">
- <span class="event-tax">Type :</span><?php osu_list_terms_text('fttype'); ?>
- <span class="event-tax">Period :</span><?php osu_list_terms_text('ftperiod'); ?>
- <span class="event-tax">Duration :</span><?php osu_list_terms_text('ftduration'); ?>
- </div> <!-- End div.taxonomy-terms -->
- <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
- <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten-child' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
- <p class="event-date"><?php
- // Get dates of event
- $osu_start_d = get_post_meta($post->ID, 'StartEventDate', true);
- $osu_end_d = get_post_meta($post->ID, 'EndEventDate', true);
- // Display date range in correct order (function reverses date order)
- osu_showdates($osu_start_d, $osu_end_d);
- ?>
- </p>
- <?php the_excerpt(); ?>
- <div class="entry-utility">
- <span class="comments-link">
- <?php // Hide comments pop up if none available
- if ( comments_open() ) {
- comments_popup_link( __( 'Leave a comment', 'twentyten-child' ), __( '1 Comment', 'twentyten-child' ), __( '% Comments', 'twentyten-child' ) );
- }
- ?></span>
- <?php edit_post_link( __( 'Edit', 'twentyten-child' ), '<span class="edit-link">', '</span>' ); ?>
- </div><!-- .entry-utility -->
- </div><!-- #post-## -->
- <?php comments_template( '', true ); ?>
- <?php endwhile; ?>
- <?php endif; /* Otherwise, no posts available */ ?>
- <?php
- /*
- ******************************************************************
- // !IF NO POSTS
- ******************************************************************
- */
- /* If there are no posts to display, such as an empty archive page */
- if ( ! have_posts() ) : ?>
- <div id="post-0" class="post error404 not-found">
- <h1 class="entry-title"><?php _e( 'No listings found', 'twentyten-child' ); ?></h1>
- <div class="entry-content">
- <p><?php _e( 'Apologies, but no tours or events were found using those filter options. Please try again.', 'twentyten-child' ); ?></p>
- </div><!-- .entry-content -->
- </div><!-- #post-0 -->
- <?php endif; ?>
- <?php
- /*
- ******************************************************************
- // !NAVIGATION LINKS
- ******************************************************************
- */
- ?>
- <?php /* Display navigation to next/previous pages when applicable - NOTE, NAV SWITCHED AROUND AS WANT FUTURE POSTS LINKS TO APPEAR TO THE RIGHT, NOT THE LEFT (MAKES MORE SENSE VISUALLY) */ ?>
- <?php if ( $wp_query->max_num_pages > 1 ) : ?>
- <div id="nav-below" class="navigation">
- <div class="nav-previous"><?php next_posts_link( __( 'Future events <span class="meta-nav">></span>', 'twentyten-child' ) ); ?></div>
- <div class="nav-next"><?php previous_posts_link( __( '<span class="meta-nav"><</span> Earlier events', 'twentyten-child' ) ); ?></div>
- </div><!-- #nav-above -->
- <?php endif; ?>