View file StuffPost/stuffpost/functions/widget/widget-latest.php

File size: 2.73Kb
<?php
/**
 * Plugin Name: Latest posts
 */

// Register and load the widget
function weart_load_latestposts() { register_widget( 'weart_latestposts' ); }
add_action( 'widgets_init', 'weart_load_latestposts' );

// Creating the widget
class weart_latestposts extends WP_Widget {

  function __construct() {
    parent::__construct(
      /* Base   ID   */ 'weart_latestposts',
      /* Widget name */ esc_html__('WeartStudio: Latest Posts - THEME SHARED ON THEMELOCK.COM', 'stuffpost')
    );
  }

  // DISPLAY
    public function widget( $args, $instance ) {

      global $post;
      $title = apply_filters('widget_title', $instance['title'] );
      $number = $instance['number'];

      // before and after widget arguments are defined by themes
      echo wpautop( $args['before_widget'] );
      // if widget has title
      if ( $title ) { echo wpautop( $args['before_title'] . $title . $args['after_title'] ); }

      $i = 1; $recent = new WP_Query(array( 'posts_per_page' => $number )); while($recent->have_posts()) : $recent->the_post(); ?>

          <?php weart_itemList( array('cat'=>0,'circle'=>1) ) ?><hr>

      <?php $i++; endwhile; wp_reset_postdata(); ?>


      <?php //end of the widget displaying
      echo wpautop( $args['after_widget'] );
    }

  // FORM
    public function form( $instance ) {
      //defaults
        $defaults = array( 'title' => esc_html__('Fresh','stuffpost'), 'number' => 5, 'popular_days' => 30 );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>

      <p><!-- title -->
        <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e('Title:','stuffpost') ?></label>
        <input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' )); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" style="width:90%;" />
      </p><!-- title -->

      <p><!-- posts num -->
        <label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php esc_html_e('Number of posts to display:','stuffpost'); ?></label>
        <input id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" value="<?php echo esc_attr( $instance['number'] ); ?>" size="3" />
      </p><!-- posts num -->
    <?php }

  // UPDATING DATAS
    public function update( $new_instance, $old_instance ) {
      $instance = array();
      $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
      $instance['number'] = ( ! empty( $new_instance['number'] ) ) ? strip_tags( $new_instance['number'] ) : '';
      return $instance;
    }

}