Skip to content

Instantly share code, notes, and snippets.

@joshbenham
Created November 27, 2013 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshbenham/e03c35313282c6ac402a to your computer and use it in GitHub Desktop.
Save joshbenham/e03c35313282c6ac402a to your computer and use it in GitHub Desktop.
<?php
add_action( 'widgets_init', create_function( '', 'register_widget("Best_Sellers_Widget");'));
class Best_Sellers_Widget extends WP_Widget {
static $items = array('first', 'second', 'third');
public function __construct() {
$widget_options = array(
'classname' => 'best_sellers',
'description' => 'A list of products that are your best sellers'
);
parent::__construct("best_sellers", "Best Sellers", $widget_options);
}
public function form($instance) {
$instance = wp_parse_args(
(array)$instance,
array(
'title' => '',
'first' => '',
'second' => '',
'third' => ''
)
);
$form = sprintf(
'<p>
<label for="%s">Title</label>
<input id="%s" name="%s" value="%s" class="widefat" type="text" />
</p>',
$this->get_field_id('title'),
$this->get_field_id('title'),
$this->get_field_name('title'),
$instance['title']
);
$products = get_posts(array('post_type' => 'product'));
foreach (self::$items as $item) {
$form .= sprintf(
'<div><label for="%s">%s Best Seller</label><select id="%s" name="%s"><option value=""></option>',
$this->get_field_id($item),
ucfirst($item),
$this->get_field_id($item),
$this->get_field_name($item)
);
foreach($products as $product) {
$form .= sprintf(
'<option value="%s" %s>%s</option>',
$product->ID,
($instance[$item] == $product->ID) ? 'selected' : '',
$product->post_title
);
}
$form .= sprintf('</select></div>');
}
echo $form;
}
public function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
foreach (self::$items as $item)
$instance[$item] = $new_instance[$item];
return $instance;
}
public function widget($args, $instance) {
global $post;
extract($args, EXTR_SKIP);
$title = apply_filters('widget_title', $instance['title'] );
echo $before_widget;
echo '<ul>';
if ($title) echo $before_title . $title . $after_title;
echo '<ul class="small-block-grid-1 large-block-grid-3">';
foreach (self::$items as $item) {
$post = get_post($instance[$item]);
setup_postdata($post);
echo sprintf('
<li>
<h4><a href="%s">%s</a></h4>
<a href="%s">%s</a>
</li>',
get_permalink(),
get_the_title(),
get_permalink(),
"asdasd"
);
}
echo '</ul>';
echo '</ul>';
echo $after_widget;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment