__( 'A widget that shows Random posts', 'accesspress-mag' ) ) ); } /** * Helper function that holds widget fields * Array is used in update and form functions */ private function widget_fields() { $fields = array( 'random_posts_title' => array( 'accesspress_mag_widgets_name' => 'random_posts_title', 'accesspress_mag_widgets_title' => __( 'Title', 'accesspress-mag' ), 'accesspress_mag_widgets_field_type' => 'title', ), 'random_posts_count' => array( 'accesspress_mag_widgets_name' => 'random_posts_count', 'accesspress_mag_widgets_title' => __( 'Number of Posts', 'accesspress-mag' ), 'accesspress_mag_widgets_field_type' => 'select', 'accesspress_mag_widgets_field_options' => array( '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',) ), ); return $fields; } /** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) { extract( $args ); $random_posts_title = $instance['random_posts_title']; $random_posts_count = $instance['random_posts_count']; echo $before_widget; ?>

'post','post_status'=>'publish','posts_per_page'=>$random_posts_count,'orderby'=>'rand' ); $rand_posts_query = new WP_Query( $rand_posts_args ); if( $rand_posts_query->have_posts() ) { while( $rand_posts_query->have_posts() ) { $rand_posts_query->the_post(); $image_id = get_post_thumbnail_id(); $image_path = wp_get_attachment_image_src( $image_id, 'accesspress-mag-block-small-thumb', true ); $image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true ); ?>
<?php echo esc_attr( $image_alt );?> <?php _e( 'No image', 'accesspress-mag' );?>

widget_fields(); // Loop through fields foreach ($widget_fields as $widget_field) { extract($widget_field); // Use helper function to get updated field values $instance[$accesspress_mag_widgets_name] = accesspress_mag_widgets_updated_field_value($widget_field, $new_instance[$accesspress_mag_widgets_name]); } return $instance; } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. * * @uses accesspress_pro_widgets_show_widget_field() defined in widget-fields.php */ public function form($instance) { $widget_fields = $this->widget_fields(); // Loop through fields foreach ($widget_fields as $widget_field) { // Make array elements available as variables extract($widget_field); $accesspress_mag_widgets_field_value = !empty($instance[$accesspress_mag_widgets_name]) ? esc_attr($instance[$accesspress_mag_widgets_name]) : ''; accesspress_mag_widgets_show_widget_field($this, $widget_field, $accesspress_mag_widgets_field_value); } } }