File size: 3.26Kb
<?php
/**
* Plugin Name: Latest posts
*/
// Register and load the widget
function weart_load_categoryposts() { register_widget( 'weart_categoryposts' ); }
add_action( 'widgets_init', 'weart_load_categoryposts' );
// Creating the widget
class weart_categoryposts extends WP_Widget {
function __construct() {
parent::__construct(
/* Base ID */ 'weart_categoryposts',
/* Widget name */ esc_html__('WeartStudio: Category Posts', 'stuffpost')
);
}
// DISPLAY
public function widget( $args, $instance ) {
global $post;
$title = apply_filters('widget_title', $instance['title'] );
$cat = get_cat_ID( $instance['cat'] );;
$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, 'cat' => $cat )); 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__('Posts from Category','stuffpost'), 'number' => 5,'cat'=>'', '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 cat -->
<label for="<?php echo esc_attr($this->get_field_id( 'cat' )); ?>"><?php esc_html_e('Category name:','stuffpost'); ?></label>
<input id="<?php echo esc_attr($this->get_field_id( 'cat' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'cat' )); ?>" value="<?php echo esc_attr($instance['cat']); ?>" style="width:90%;" />
</p><!-- posts cat -->
<p><!-- posts num -->
<label for="<?php echo esc_attr($this->get_field_id( 'number' )); ?>"><?php esc_html_e('Number of posts:','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['cat'] = ( ! empty( $new_instance['cat'] ) ) ? strip_tags( $new_instance['cat'] ) : '';
$instance['number'] = ( ! empty( $new_instance['number'] ) ) ? strip_tags( $new_instance['number'] ) : '';
return $instance;
}
}