Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 25th, 2012  |  syntax: None  |  size: 9.88 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php /**
  2.  * Custom Post Type ftevent (Events) loop template
  3.  * IMPORTANT NOTE: WORDPRESS 3.1 READY. USES:
  4.  * - meta_query (instead of meta_key/value/compare)
  5.  * - tax_query (instead of Multiple Taxonomy Query plugin)
  6.  *
  7.  */
  8. ?>
  9.  
  10. <?php
  11. /*
  12. ******************************************************************
  13.                         // !SET TAXONOMY TERMS IF FORM POSTED
  14. ******************************************************************
  15. */
  16.  
  17. // FUNCTIONS USED IN THIS SCRIPT
  18.  
  19. // List taxonomy terms as an option list and recall options chosen if applicable
  20. function osu_list_terms($osu_tax, $osu_recall) {
  21.         // list terms in taxonomy term
  22.         $tax_terms = get_terms($osu_tax);
  23.         // if $osu_recall is true, recall selected options
  24.        
  25.         foreach ($tax_terms as $tax_term) {
  26.             echo '<option';
  27.             // Check if option chosen is the current <option> being created
  28.             if ($osu_recall == $tax_term->name) {
  29.                         echo ' selected="yes"';
  30.                 }
  31.             echo '>' . $tax_term->name;
  32.             echo '</option>';
  33.         }
  34. }
  35.  
  36. // Function to echo out date range for Event Custom Post types - Needs CF start
  37. // and end dates passed to it and relies on osu_date_format() function.
  38. function osu_showdates($osu_start_d, $osu_end_d) {
  39.        
  40.         // Reverse the StartEventDate and EndEventDate Custom fields so they display in correct order (dd/mm/yyyy not yyyy/mm/dd)
  41.         $osu_start_d_rev = osu_date_format($osu_start_d);
  42.         $osu_end_d_rev = osu_date_format($osu_end_d);
  43.        
  44.         // No need to display reversed EndEventDate date if event happens on one day only
  45.         if($osu_end_d !== $osu_start_d || $osu_end_d == '') {
  46.             $osu_end_d_rev = ' - ' . $osu_end_d_rev;
  47.         }
  48.        
  49.         // If no start date at all, display error message
  50.         if(!$osu_start_d) {
  51.             echo '<span>Event date not available!</span>';
  52.         } else {
  53.             echo $osu_start_d_rev . $osu_end_d_rev;
  54.         }
  55. }
  56.  
  57. // Function to convert spaces to hyphens (turn string into slug)
  58. function osu_convert_spaces($convert) {
  59.         $converted = preg_replace("/\s/","-",strtolower($convert));
  60.         return $converted;
  61. }
  62.  
  63. // Get post data from filter
  64. if(isset($_POST["fttype"])) {
  65.         $ft_t = $_POST['fttype'];
  66. } else {
  67.         $ft_t = '';
  68. }
  69. if(isset($_POST["ftperiod"])) {
  70.         $ft_p = $_POST['ftperiod'];
  71. } else {
  72.         $ft_p = '';
  73. }
  74. if(isset($_POST["ftduration"])) {
  75.         $ft_d = $_POST['ftduration'];
  76. } else {
  77.         $ft_d = '';
  78. }
  79.  
  80. // If 'All' chosen in options, make option blank so all taxonomy terms returned
  81. if($ft_t == 'All')
  82.         { $ft_t = ''; }
  83. if($ft_p == 'All')
  84.         { $ft_p = ''; }
  85. if($ft_d == 'All')
  86.         { $ft_d = ''; }
  87.  
  88. ?>
  89.  
  90. <?php
  91. /*
  92. ******************************************************************
  93.                                         // !SET THE QUERY
  94. ******************************************************************
  95. */
  96.  
  97. // QUESTION: Paged results - for some reason pagination doesn't appear
  98. // on the results page when the filter form is submitted??
  99. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  100.  
  101. // Because pagination isn't working once the form is submitted, we'll show all posts (uses hidden input)
  102. if($_POST['showall']) {
  103.         $ft_ppp = 100;
  104. } else {
  105.         $ft_ppp = 3;
  106. }
  107.  
  108. // Set todays date to check against the custom field StartEventDate
  109. $todaysDate = date('Y/m/d');
  110.  
  111. // Convert spaces in taxonomies and terms into hyphens so that search works correctly (uses slug)
  112. $ft_t_ns = osu_convert_spaces($ft_t);
  113. $ft_p_ns = osu_convert_spaces($ft_p);
  114. $ft_d_ns = osu_convert_spaces($ft_d);
  115.  
  116. // Build query - WP 3.1 only!
  117. // READ MORE ON 'MULTIPLE TAXONOMY HANDLING' HERE:
  118. // http://codex.wordpress.org/Function_Reference/query_posts#Taxonomy_Parameters
  119. $ft_args = array(
  120.         'post_type' => 'ftevent',
  121.         'posts_per_page' => $ft_ppp,
  122.         'paged' => $paged,
  123.         // 'meta_key' is needed to 'orderby' posts by Custom Field StartEventDate
  124.         'meta_key' => 'StartEventDate',
  125.         'orderby' => 'meta_value',
  126.         'order' => 'ASC',
  127.         // Use meta_query to filter results
  128.         'meta_query' => array(
  129.                 array(
  130.                         'key' => 'StartEventDate',
  131.                         'value' => $todaysDate,
  132.                         // Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL',
  133.                         // 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'.
  134.                         'type' => 'DATE',
  135.                         // Operator to test. Possible values are 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
  136.                         // We choose 'BETWEEN' because we need to know the date has not passed to show the event
  137.                         'compare' => '>='
  138.                 )
  139.         ),
  140.         'tax_query' => array(
  141.                 'relation' => 'AND',
  142.                 array(
  143.                         'taxonomy' => 'fttype',
  144.                         'field' => 'slug',
  145.                         'terms' => array($ft_t_ns),
  146.                         // Operator to test. Possible values are 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
  147.                         // We choose 'IN' because we need to make sure the term is in the current array of posts
  148.                         'operator' => 'LIKE',
  149.                 ),
  150.                 array(
  151.                         'taxonomy' => 'ftperiod',
  152.                         'field' => 'slug',
  153.                         'terms' => array($ft_p_ns),
  154.                         'operator' => 'LIKE',
  155.                 ),
  156.                 array(
  157.                         'taxonomy' => 'ftduration',
  158.                         'field' => 'slug',
  159.                         'terms' => array($ft_d_ns),
  160.                         'operator' => 'LIKE',
  161.                 ),
  162.         )
  163. );
  164.  
  165. // Create query
  166. query_posts($ft_args);
  167. ?>
  168.  
  169. <?php
  170. /*
  171. ******************************************************************
  172.                                                 // !FILTER FORM
  173. ******************************************************************
  174. */
  175. ?>
  176.  
  177. <?php
  178.         // Check values in array
  179.         print '<p><pre>';
  180.         print_r( $ft_args );
  181.         print '</pre></p>';
  182. ?>
  183.  
  184. <form action="/ftevent/" method="post" id="ftfilter">
  185.         <fieldset>
  186.                 <legend>Filter</legend>
  187.                 <ul>
  188.                         <li>
  189.                                 <label>Type</label>
  190.                                 <select name="fttype" id="fttype-select"><option>All</option>
  191.                                 <?php osu_list_terms('fttype', $ft_t); ?>
  192.                                 </select>
  193.                         </li>
  194.                         <li>
  195.                                 <label>Period</label>
  196.                                 <select name="ftperiod" id="ftperiod-select"><option>All</option>
  197.                                 <?php osu_list_terms('ftperiod', $ft_p); ?>
  198.                                 </select>
  199.                         </li>
  200.                         <li>
  201.                                 <label>Duration</label>
  202.                                 <select name="ftduration" id="ftduration-select"><option>All</option>
  203.                                 <?php osu_list_terms('ftduration', $ft_d); ?>
  204.                                 </select>
  205.                         </li>
  206.                         <li class="end">
  207.                                 <input type="submit" id="filtersubmit" value="Submit" />
  208.                                 <input type="hidden" id="showall" name="showall" value="true" />
  209.                         </li>
  210.                 </ul>
  211.         </fieldset>
  212. </form>
  213.  
  214. <?php
  215. /*
  216. ******************************************************************
  217.                                                 // !LOOP STARTS
  218. ******************************************************************
  219. */
  220. ?>
  221.  
  222. <?php if ( have_posts() ) : ?>
  223.  
  224.         <?php while ( have_posts() ) : the_post(); ?>
  225.  
  226.                 <div class="event-thumbnail">
  227.                         <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten-child' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php
  228.                         if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) {
  229.                                 the_post_thumbnail('thumbnail');
  230.                         } else { ?>
  231.                                 <img src="<?php echo bloginfo('stylesheet_directory'); ?>/images/thumb-default.jpg" alt="thumb-default" width="65" height="65" />
  232.                         <?php } ?>
  233.                 </a>
  234.         </div> <!-- End div.event-thumbnail -->
  235.         <div class="taxonomy-terms">
  236.                 <span class="event-tax">Type :</span><?php osu_list_terms_text('fttype'); ?>
  237.                 <span class="event-tax">Period :</span><?php osu_list_terms_text('ftperiod'); ?>
  238.                 <span class="event-tax">Duration :</span><?php osu_list_terms_text('ftduration'); ?>
  239.         </div> <!-- End div.taxonomy-terms -->
  240.  
  241.                 <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  242.                         <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>                      
  243.                         <p class="event-date"><?php
  244.                                 // Get dates of event
  245.                                 $osu_start_d = get_post_meta($post->ID, 'StartEventDate', true);
  246.                                 $osu_end_d = get_post_meta($post->ID, 'EndEventDate', true);
  247.                                
  248.                                 // Display date range in correct order (function reverses date order)
  249.                                 osu_showdates($osu_start_d, $osu_end_d);
  250.                                 ?>
  251.                         </p>
  252.                        
  253.                         <?php the_excerpt(); ?>
  254.  
  255.                         <div class="entry-utility">
  256.                                 <span class="comments-link">
  257.                                         <?php // Hide comments pop up if none available
  258.                                                 if ( comments_open() ) {
  259.                                                         comments_popup_link( __( 'Leave a comment', 'twentyten-child' ), __( '1 Comment', 'twentyten-child' ), __( '% Comments', 'twentyten-child' ) );
  260.                                                 }
  261.                                         ?></span>
  262.                                 <?php edit_post_link( __( 'Edit', 'twentyten-child' ), '<span class="edit-link">', '</span>' ); ?>
  263.                         </div><!-- .entry-utility -->
  264.                 </div><!-- #post-## -->
  265.  
  266.                 <?php comments_template( '', true ); ?>
  267.                
  268.         <?php endwhile;  ?>
  269.  
  270. <?php endif; /* Otherwise, no posts available */ ?>
  271.  
  272. <?php
  273. /*
  274. ******************************************************************
  275.                                                 // !IF NO POSTS
  276. ******************************************************************
  277. */
  278.  
  279. /* If there are no posts to display, such as an empty archive page */
  280. if ( ! have_posts() ) : ?>
  281.         <div id="post-0" class="post error404 not-found">
  282.                 <h1 class="entry-title"><?php _e( 'No listings found', 'twentyten-child' ); ?></h1>
  283.                 <div class="entry-content">
  284.                         <p><?php _e( 'Apologies, but no tours or events were found using those filter options. Please try again.', 'twentyten-child' ); ?></p>
  285.                 </div><!-- .entry-content -->
  286.         </div><!-- #post-0 -->
  287. <?php endif; ?>
  288.  
  289.  
  290.  
  291. <?php
  292. /*
  293. ******************************************************************
  294.                                         // !NAVIGATION LINKS
  295. ******************************************************************
  296. */
  297. ?>
  298.  
  299.  
  300. <?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) */ ?>
  301. <?php if ( $wp_query->max_num_pages > 1 ) : ?>
  302.         <div id="nav-below" class="navigation">
  303.                 <div class="nav-previous"><?php next_posts_link( __( 'Future events <span class="meta-nav">&gt;</span>', 'twentyten-child' ) ); ?></div>
  304.                 <div class="nav-next"><?php previous_posts_link( __( '<span class="meta-nav">&lt;</span> Earlier events', 'twentyten-child' ) ); ?></div>
  305.         </div><!-- #nav-above -->
  306. <?php endif; ?>