Skip to content

Instantly share code, notes, and snippets.

@jo-snips
Created September 18, 2012 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jo-snips/3743686 to your computer and use it in GitHub Desktop.
Save jo-snips/3743686 to your computer and use it in GitHub Desktop.
The Events Calendar: Random Event Widget
<?php
/*-----------------------------------------------------------------------------------*/
/* Random Event Widget
/*-----------------------------------------------------------------------------------*/
add_action( 'widgets_init', create_function( '', "register_widget( 'Random_Event_Widget' );" ) );
class Random_Event_Widget extends WP_Widget {
function __construct()
{
$widget_ops = array(
'classname' => 'widget_random_events',
'description' => __( 'Displays a random event in the next 2 weeks.' )
);
parent::__construct( 'widget_random_events', __( 'Random Event Widget' ), $widget_ops );
}
function form( $instance )
{
$title = esc_attr( isset( $instance['title'] ) ? $instance['title'] : 'Random Event' );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</label>
</p>
<?php
}
function update( $new_instance, $old_instance )
{
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
function widget( $args, $instance )
{
extract($args);
$title = $instance['title'];
echo $before_widget;
if ( $title ) {
echo $before_title . $title . $after_title;
}
?>
<!-- begin widget content -->
<ul>
<?php
$current_date = date('Y-m-d H:i:s', strtotime('today'));
$end_date = date('Y-m-d H:i:s', strtotime('+2 weeks'));
echo 'Start Date:'. $current_date;
echo 'End Date:'. $end_date;
$args = array(
'post_type' => array( 'tribe_events' ),
'posts_per_page' => 1,
'orderby' => 'rand',
'meta_query', array(
array(
'key' => '_EventStartDate',
'value' => $current_date,
'compare' => '>'
),
array(
'key' => '_EventEndDate',
'value' => $end_date,
'compare' => '<'
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<!-- START post -->
<li class="clearfix">
<div class="entry-date">
<span class="month"><?php the_time('M'); ?></span>
<span class="day"><?php the_time('j'); ?></span>
<span class="year"><?php the_time('Y'); ?></span>
</div>
<div class="entry-content">
<h5><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h5>
<?php echo get_the_content(); ?>
<a href="<?php the_permalink(); ?>">Read more &raquo;</a>
</div>
</li>
<!-- END post -->
<?php endwhile; wp_reset_query(); ?>
</ul>
</div>
<!-- end widget content -->
<?php
echo $after_widget;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment