'', 'category' => '', 'random' => '', 'count' => 3, ); public function __construct() { $widget_ops = array( 'description' => esc_html__( 'Displays a number of the latest (or random) posts from a specific category.', 'brittany-light' ) ); $control_ops = array(); parent::__construct( 'ci-latest-posts', esc_html__( 'Theme - Latest Posts', 'brittany-light' ), $widget_ops, $control_ops ); } public function widget( $args, $instance ) { $instance = wp_parse_args( (array) $instance, $this->defaults ); $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); $category = $instance['category']; $random = (int) $instance['random']; $count = (int) $instance['count']; if ( 0 === $count ) { return; } $qargs = array( 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => $count, 'ignore_sticky_posts' => true, ); if ( 1 === $random ) { $qargs['orderby'] = 'rand'; unset( $qargs['order'] ); } if ( ! empty( $category ) && $category > 0 ) { $qargs['cat'] = intval( $category ); } $q = new WP_Query( $qargs ); if ( $q->have_posts() ) { echo $args['before_widget']; if ( ! empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; } while ( $q->have_posts() ) { $q->the_post(); get_template_part( 'item-media', get_post_type() ); } wp_reset_postdata(); echo $args['after_widget']; } } public function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = sanitize_text_field( $new_instance['title'] ); $instance['category'] = brittany_light_sanitize_intval_or_empty( $new_instance['category'] ); $instance['random'] = brittany_light_sanitize_checkbox_ref( $new_instance['random'] ); $instance['count'] = intval( $new_instance['count'] ) > 0 ? intval( $new_instance['count'] ) : 1; return $instance; } public function form( $instance ) { $instance = wp_parse_args( (array) $instance, $this->defaults ); $title = $instance['title']; $category = $instance['category']; $random = $instance['random']; $count = $instance['count']; ?>

'category', 'show_option_all' => '', 'show_option_none' => ' ', 'option_none_value' => '', 'show_count' => 1, 'echo' => 1, 'selected' => $category, 'hierarchical' => 1, 'name' => $this->get_field_name( 'category' ), 'id' => $this->get_field_id( 'category' ), 'class' => 'postform widefat', ) ); ?>