Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2011
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.57 KB | None | 0 0
  1. <?php
  2. class duv_latest_cpt extends WP_Widget
  3. {
  4.  
  5.     /**
  6.      * PHP5 Constructor
  7.      */
  8.     function __construct()
  9.     {
  10.  
  11.         $widget_ops = array(
  12.                                 'classname'     => 'duv_latest_cpt',
  13.                                 'description'   => __('Display latests custom posts posts')
  14.                             );
  15.         parent::__construct('lcpt', __('Latest Custom Post posts'), $widget_ops);
  16.  
  17.     }
  18.  
  19.  
  20.     /**
  21.      * Echo the widget content.
  22.      *
  23.      * Subclasses should over-ride this function to generate their widget code.
  24.      *
  25.      * @param array $args       Display arguments including before_title,
  26.      *                              before_widget, and after_widget.
  27.      * @param array $instance   The settings for the particular instance of the
  28.      *                              widget
  29.      */
  30.     function widget($args, $instance)
  31.     {
  32.  
  33.         extract($args);
  34.  
  35.         $title  = empty($instance['title']) ? '' : $instance['title'];
  36.         $cpt        = empty($instance['cpt']) ? '' : $instance['cpt'];
  37.         $num        = empty($instance['num']) ? '' : $instance['num'];
  38.  
  39.         echo $before_widget;
  40.  
  41.         // display title
  42.         if($title != '') echo '<h2>' . $title .'</h2>';
  43.  
  44.  
  45.         global $post;
  46.         $arguments  = array('public' => 'true');
  47.         // retrieve all CPT names that are accessible from non-admin (public)
  48.         $cpt    = get_post_types($arguments, 'names');
  49.         foreach($cpt as $t):
  50.             echo $t;
  51.         endforeach;
  52.  
  53.         // dispay posts
  54.         if($cpt != ''):
  55.             $q = new WP_Query(array(
  56.                 'post_type' => $cpt,
  57.                 'showposts' => $num
  58.             ));
  59.  
  60.             if($q->have_posts()):
  61.  
  62.                 // there are post
  63.  
  64.                 while($q->have_posts()) : $q->the_post();
  65.  
  66.                     // display information
  67.  
  68.                 endwhile;
  69.  
  70.                 // we outside the loop, need to close a div or sometthing ?
  71.  
  72.  
  73.             else:
  74.  
  75.                 // there were no posts
  76.  
  77.             endif;
  78.  
  79.         endif;
  80.  
  81.         echo $after_widget;
  82.  
  83.     }
  84.  
  85.  
  86.     /**
  87.      * Update a particular instance.
  88.      *
  89.      * This function should check taht $new_instance is set correctly.
  90.      * The newly calculated vallue of $instance should be returned.
  91.      * If "false" is returned, the instance won't be saved/updated.
  92.      *
  93.      * @param   array $new_instance     New settings for this instance as input bu the
  94.      *                                      user via form()
  95.      * @param   array $old_instance     Old settings for this instance
  96.      * @return  array                   Settings to save or bool false to cancel saving
  97.      */
  98.     function update($new_instance, $old_instance)
  99.     {
  100.  
  101.         $instance = $old_instance;
  102.  
  103.         // title
  104.         $instance['title'] = strip_tags($new_instance['title']);
  105.  
  106.         // custom post type
  107.         $args   = array('public' => true, '_builtin' => false);
  108.         // retrieve all CPT names that are accessible from non-admin (public)
  109.         $cpt    = get_post_types($args, 'names');
  110.         if(in_array($new_instance['cpt'], $cpt)):
  111.             $instance['cpt'] = $new_instance['cpt'];
  112.         else:
  113.             $instance['cpt'] = '';
  114.         endif;
  115.  
  116.         // number of post to show
  117.         $instance['num'] = strip_tags($new_instance['num']);
  118.  
  119.         return $instance;
  120.  
  121.     }
  122.  
  123.  
  124.     /**
  125.      * Echo the settings update form
  126.      *
  127.      * @param array $instance Current settings
  128.      */
  129.     function form($instance)
  130.     {
  131.  
  132.         $instance = wp_parse_args( (array) $instance, array(
  133.             'title'     => '',
  134.             'cpt'   => '',
  135.             'num'   => '1')
  136.         );
  137.         $title  = esc_attr($instance['title']);
  138.         $cpt        = esc_attr($instance['cpt']);
  139.         $num        = esc_attr($instance['num']);
  140.         ?>
  141.  
  142.         <!-- TITLE -->
  143.         <p>
  144.             <label for="<?php $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  145.             <input  class="widefat"
  146.                         id="<?php echo $this->get_field_id('title'); ?>"
  147.                         name="<?php echo $this->get_field_name('title'); ?>"
  148.                         type="text"
  149.                         value="<?php echo $title; ?>">
  150.         </p>
  151.  
  152.         <!-- SELECT CUTSTOM POST TYPE -->
  153.         <p>
  154.             <label for"<?php echo $this->get_field_id('cpt'); ?>"><?php _e('Select Custom Post Type'); ?></label>
  155.             <select     class="widefat"
  156.                         name="<?php echo $this->get_field_name('cpt'); ?>"
  157.                         id="<?php echo $this->get_field_id('cpt'); ?>">
  158.                 <?php
  159.                 $args   = array('public' => true, '_builtin' => false);
  160.                 // retrieve all CPT names that are accessible from non-admin (public)
  161.                 $cpt    = get_post_types($args, 'names');
  162.                 foreach($cpt as $t):
  163.                 ?>
  164.                     <option     value="<?php echo $t; ?>"
  165.                                 <?php selected($instance['cpt'], $this->get_field_id('cpt')); ?>>
  166.                         <?php _e($t); ?>
  167.                     </option>
  168.                 <?php
  169.                 endforeach;
  170.                 ?>
  171.             </select>
  172.         </p>
  173.  
  174.         <!-- NUMBER OF POSTS TO SHOW -->
  175.         <p>
  176.             <label for="<?php $this->get_field_id('num'); ?>"><?php _e('Number of posts to show'); ?></label>
  177.             <input  id="<?php echo $this->get_field_id('num'); ?>"
  178.                         name="<?php echo $this->get_field_name('num'); ?>"
  179.                         type="text"
  180.                         value="<?php echo $num ?>"
  181.                         size="2">
  182.             <br />
  183.             <small><?php _e('Tip: enter -1 to show all posts'); ?></small>
  184.         </p>
  185.         <?php
  186.  
  187.     }
  188.  
  189. }
  190.  
  191.  
  192. function duv_register_widget()
  193. {
  194.  
  195.     register_widget('duv_latest_cpt');
  196.  
  197.     do_action('widgets_init');
  198.  
  199. }
  200.  
  201. add_action('init', 'duv_register_widget');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement