Skip to content

Instantly share code, notes, and snippets.

@xavivars
Created September 27, 2011 17:09
Show Gist options
  • Save xavivars/1245642 to your computer and use it in GitHub Desktop.
Save xavivars/1245642 to your computer and use it in GitHub Desktop.
Woothemes Woo Widget file
<?php
/*---------------------------------------------------------------------------------*/
/* Photos CPT Widget */
/*---------------------------------------------------------------------------------*/
class WP_Widget_Photos extends WP_Widget {
function WP_Widget_Photos() {
$widget_ops = array('classname' => 'widget-photos', 'description' => __('Display Photos from your site.', 'woothemes'));
$control_ops = array('width' => 400, 'height' => 350);
$this->WP_Widget('woo_photos', __('Woo - Photos', 'woothemes'), $widget_ops, $control_ops);
}
function widget( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_photos_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
$w = apply_filters( 'widget_photos_w', $instance['w'], $instance );
$h = apply_filters( 'widget_photos_h', $instance['h'], $instance );
$repeat = apply_filters( 'widget_photos_repeat', $instance['repeat'], $instance );
$limit = apply_filters( 'widget_photos_limit', $instance['limit'], $instance );
$order = $instance['order'];
$orderby = $instance['orderby'];
if(empty($order)) $order = 'DESC';
if(empty($orderby)) $orderby = 'menu_order';
if(empty($w)) $w = 80;
if(empty($h)) $h = 80;
if(empty($repeat)) $repeat = 6;
if(empty($limit)) $limit = 15;
$r = new WP_Query(array('posts_per_page' => $limit, 'nopaging' => 0, 'post_status' => 'publish', 'post_type' => 'photo', 'caller_get_posts' => 1, 'order' => $order, 'orderby' => $orderby));
if ($r->have_posts()) : ?>
<?php echo $before_widget;?>
<?php
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
<ul class="widget-list">
<?php while ($r->have_posts()) : $r->the_post(); ?>
<li>
<h2><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a></h2>
<div class="photos-holder"><?php woo_image( 'width=' . $w . '&height=' . $h . '&repeat=' . $repeat . '&single=true&id=' . get_the_ID() ); ?></div>
</li>
<?php endwhile; ?>
</ul>
<?php
echo $after_widget;
endif;
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['repeat'] = $new_instance['repeat'];
$instance['w'] = $new_instance['w'];
$instance['h'] = $new_instance['h'];
$instance['orderby'] = $new_instance['orderby'];
$instance['order'] = $new_instance['order'];
$instance['limit'] = $new_instance['limit'];
return $instance;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'orderby' => '', 'title' => '', 'order' => '', 'w' => '', 'h' => '', 'limit' => '', 'repeat' => '', 'order' => '') );
$title = strip_tags($instance['title']);
$orderby = $instance['orderby'];
$order = $instance['order'];
$w = $instance['w'];
$h = $instance['h'];
$repeat = $instance['repeat'];
$limit = $instance['limit'];
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'woothemes'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
<p><label for="<?php echo $this->get_field_id('w'); ?>"><?php _e('Thumbnail Width:', 'woothemes'); ?></label>
<input class="" id="<?php echo $this->get_field_id('w'); ?>" name="<?php echo $this->get_field_name('w'); ?>" type="text" value="<?php echo $w; ?>" /></p>
<p><label for="<?php echo $this->get_field_id('h'); ?>"><?php _e('Thumbnail Height:', 'woothemes'); ?></label>
<input class="" id="<?php echo $this->get_field_id('h'); ?>" name="<?php echo $this->get_field_name('h'); ?>" type="text" value="<?php echo $h; ?>" /></p>
<p><label for="<?php echo $this->get_field_id('repeat'); ?>"><?php _e('Amount of Thumbs:', 'woothemes'); ?></label>
<input class="" id="<?php echo $this->get_field_id('repeat'); ?>" name="<?php echo $this->get_field_name('repeat'); ?>" type="text" value="<?php echo $repeat; ?>" /></p>
<p><label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e('Post Limit:', 'woothemes'); ?></label>
<input class="" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit; ?>" /></p>
<p><label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e('Post Orderby:', 'woothemes'); ?></label>
<input class="" id="<?php echo $this->get_field_id('orderby'); ?>" name="<?php echo $this->get_field_name('orderby'); ?>" type="text" value="<?php echo $orderby; ?>" /></p>
<p><label for="<?php echo $this->get_field_id('order'); ?>"><?php _e('Post Order:', 'woothemes'); ?></label>
<input class="" id="<?php echo $this->get_field_id('order'); ?>" name="<?php echo $this->get_field_name('order'); ?>" type="text" value="<?php echo $order; ?>" /></p>
<?php
}
}
register_widget('WP_Widget_Photos');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment