jamesabruce

Wordpress widget

Jul 16th, 2011
785
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class LatestProductsWidget extends WP_Widget
  2.     {
  3.       function LatestProductsWidget()
  4.       {
  5.         $widget_ops = array('classname' => 'LatestProductsWidget', 'description' => 'Displays the latest product additions with thumbnail' );
  6.         $this->WP_Widget('LatestProductsWidget', 'Latest Products and Thumbnail', $widget_ops);
  7.       }
  8.      
  9.       function form($instance)
  10.       {
  11.         $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
  12.         $title = $instance['title'];
  13.     ?>
  14.       <p><label for="<?php echo $this->get_field_id('title'); ?>">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 attribute_escape($title); ?>" /></label></p>
  15.     <?php
  16.       }
  17.      
  18.       function update($new_instance, $old_instance)
  19.       {
  20.         $instance = $old_instance;
  21.         $instance['title'] = $new_instance['title'];
  22.         return $instance;
  23.       }
  24.      
  25.       function widget($args, $instance)
  26.       {
  27.         extract($args, EXTR_SKIP);
  28.      
  29.         echo $before_widget;
  30.         $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
  31.      
  32.         if (!empty($title))
  33.           echo $before_title . $title . $after_title;;
  34.      
  35.         // WIDGET CODE GOES HERE
  36.         query_posts('post_type=products&posts_per_page=3&order=DESC&meta_key=Rating&orderby=meta_value');
  37.         if (have_posts()) :
  38.             while (have_posts()) : the_post();
  39.                 echo "<a href='".get_permalink()."'>";
  40.                 the_title('<h2>','</h2>');
  41.                 the_post_thumbnail('thumbnail');
  42.                 the_meta();
  43.                 echo "</a>";
  44.             endwhile;
  45.         endif;
  46.         wp_reset_query();
  47.      
  48.         echo $after_widget;
  49.       }
  50.  
  51.     }
  52.     add_action( 'widgets_init', create_function('', 'return register_widget("LatestProductsWidget");') );
RAW Paste Data