Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class duv_latest_cpt extends WP_Widget
- {
- /**
- * PHP5 Constructor
- */
- function __construct()
- {
- $widget_ops = array(
- 'classname' => 'duv_latest_cpt',
- 'description' => __('Display latests custom posts posts')
- );
- parent::__construct('lcpt', __('Latest Custom Post posts'), $widget_ops);
- }
- /**
- * Echo the widget content.
- *
- * Subclasses should over-ride this function to generate their widget code.
- *
- * @param array $args Display arguments including before_title,
- * before_widget, and after_widget.
- * @param array $instance The settings for the particular instance of the
- * widget
- */
- function widget($args, $instance)
- {
- extract($args);
- $title = empty($instance['title']) ? '' : $instance['title'];
- $cpt = empty($instance['cpt']) ? '' : $instance['cpt'];
- $num = empty($instance['num']) ? '' : $instance['num'];
- echo $before_widget;
- // display title
- if($title != '') echo '<h2>' . $title .'</h2>';
- global $post;
- $arguments = array('public' => 'true');
- // retrieve all CPT names that are accessible from non-admin (public)
- $cpt = get_post_types($arguments, 'names');
- foreach($cpt as $t):
- echo $t;
- endforeach;
- // dispay posts
- if($cpt != ''):
- $q = new WP_Query(array(
- 'post_type' => $cpt,
- 'showposts' => $num
- ));
- if($q->have_posts()):
- // there are post
- while($q->have_posts()) : $q->the_post();
- // display information
- endwhile;
- // we outside the loop, need to close a div or sometthing ?
- else:
- // there were no posts
- endif;
- endif;
- echo $after_widget;
- }
- /**
- * Update a particular instance.
- *
- * This function should check taht $new_instance is set correctly.
- * The newly calculated vallue of $instance should be returned.
- * If "false" is returned, the instance won't be saved/updated.
- *
- * @param array $new_instance New settings for this instance as input bu the
- * user via form()
- * @param array $old_instance Old settings for this instance
- * @return array Settings to save or bool false to cancel saving
- */
- function update($new_instance, $old_instance)
- {
- $instance = $old_instance;
- // title
- $instance['title'] = strip_tags($new_instance['title']);
- // custom post type
- $args = array('public' => true, '_builtin' => false);
- // retrieve all CPT names that are accessible from non-admin (public)
- $cpt = get_post_types($args, 'names');
- if(in_array($new_instance['cpt'], $cpt)):
- $instance['cpt'] = $new_instance['cpt'];
- else:
- $instance['cpt'] = '';
- endif;
- // number of post to show
- $instance['num'] = strip_tags($new_instance['num']);
- return $instance;
- }
- /**
- * Echo the settings update form
- *
- * @param array $instance Current settings
- */
- function form($instance)
- {
- $instance = wp_parse_args( (array) $instance, array(
- 'title' => '',
- 'cpt' => '',
- 'num' => '1')
- );
- $title = esc_attr($instance['title']);
- $cpt = esc_attr($instance['cpt']);
- $num = esc_attr($instance['num']);
- ?>
- <!-- TITLE -->
- <p>
- <label for="<?php $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></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 $title; ?>">
- </p>
- <!-- SELECT CUTSTOM POST TYPE -->
- <p>
- <label for"<?php echo $this->get_field_id('cpt'); ?>"><?php _e('Select Custom Post Type'); ?></label>
- <select class="widefat"
- name="<?php echo $this->get_field_name('cpt'); ?>"
- id="<?php echo $this->get_field_id('cpt'); ?>">
- <?php
- $args = array('public' => true, '_builtin' => false);
- // retrieve all CPT names that are accessible from non-admin (public)
- $cpt = get_post_types($args, 'names');
- foreach($cpt as $t):
- ?>
- <option value="<?php echo $t; ?>"
- <?php selected($instance['cpt'], $this->get_field_id('cpt')); ?>>
- <?php _e($t); ?>
- </option>
- <?php
- endforeach;
- ?>
- </select>
- </p>
- <!-- NUMBER OF POSTS TO SHOW -->
- <p>
- <label for="<?php $this->get_field_id('num'); ?>"><?php _e('Number of posts to show'); ?></label>
- <input id="<?php echo $this->get_field_id('num'); ?>"
- name="<?php echo $this->get_field_name('num'); ?>"
- type="text"
- value="<?php echo $num ?>"
- size="2">
- <br />
- <small><?php _e('Tip: enter -1 to show all posts'); ?></small>
- </p>
- <?php
- }
- }
- function duv_register_widget()
- {
- register_widget('duv_latest_cpt');
- do_action('widgets_init');
- }
- add_action('init', 'duv_register_widget');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement