Hey everyone!
I was messing around with calling a custom WP Query on the events to create a simple list of upcoming events on my front page. I realize I could use the widget but I didn't see the need to create a new widget area in this application. Basically, I just wanted to share my experiences with getting it to work.
Here is what my code accomplishes.
- Creates a custom query from my "events" category (category id is 7 in my case). Also orders the events in ascending order.
- Compares today's date with the events end date to filter out past events.
- Displays the events events title and start date.
Here is my code.
<?php $custom = new WP_Query('cat=7&order=ASC'); ?>
<?php if ($custom->have_posts()) { ?>
<?php while($custom->have_posts()) { ?>
<?php $custom->the_post(); ?>
<?php
// Comparing dates to filter out past events
$exp_date = the_event_end_date();
$todays_date = date("F j, Y");
?>
<?php if (strtotime($todays_date) >= strtotime($exp_date)) { ?>
<?php } else { ?>
<div id="article-<?php the_ID(); ?>" class="article">
<p class="date">Event Date: <?php echo the_event_start_date(); ?></p>
<h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
<p></p>
</div>
<?php } ?>
<?php } ?>
<?php } else { ?>
<p>Sorry, there are no events.</p>
<?php } ?>
I realize that there is probably a much better way to do this (I am a newbie with PHP) so please make adjustments if necessary.