__( 'Display posts from a specific category', 'blesk' ), ) // Args ); } /** * 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 ) { $title = ( !empty( $instance['title'] ) ? esc_html( $instance['title'] ) : '' ); $count = ( !empty( $instance['count'] ) ? esc_html( $instance['count'] ) : '5' ); $icon = ( !empty( $instance['icon'] ) ? esc_html( $instance['icon'] ) : '' ); $category = ( !empty( $instance['category'] ) ? esc_html( $instance['category'] ) : '' ); echo $args['before_widget']; //Echo widget title echo $args['before_title']; if($icon) { echo ''; } echo esc_html($title) . $args['after_title']; // WP_Query arguments $query_args = array( 'posts_per_page' => $count, 'ignore_sticky_posts' => true, ); if($category) { $query_args['cat'] = $category; } // The Query $query = new WP_Query( $query_args ); // The Loop if ( $query->have_posts() ) { echo '
'; while ( $query->have_posts() ) { $query->the_post(); $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'home_post' ); $content_wrapper = ''; $fix_class = ''; if(!isset($image[0])) { $content_wrapper = ' style="padding: 15px 30px 30px 30px;"'; $fix_class = ' fixclass'; } echo '
'; if(isset($image[0])) { echo ''; } else { $content_wrapper = ' style="padding-left: 10px;"'; } echo '

'.get_the_title().'

'; } echo '
'; } else { echo '

'.__('No posts found.', 'blesk').'

'; } // Restore original Post Data wp_reset_postdata(); echo $args['after_widget']; } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. */ public function form( $instance ) { $fa_icons = blesk_get_fontawesome_icons(); $categories = get_terms('category', array('fields' => 'id=>name')); $title = ! empty( $instance['title'] ) ? sanitize_text_field( $instance['title'] ) : ''; $count = ! empty( $instance['count'] ) ? sanitize_text_field( $instance['count'] ) : '5'; $icon = ! empty( $instance['icon'] ) ? sanitize_text_field( $instance['icon'] ) : ''; $category = ! empty( $instance['category'] ) ? sanitize_text_field( $instance['category'] ) : ''; echo '

'; echo '

'; echo '

'; echo '

'; } /** * Sanitize widget form values as they are saved. * * @see WP_Widget::update() * * @param array $new_instance Values just sent to be saved. * @param array $old_instance Previously saved values from database. * * @return array Updated safe values to be saved. */ public function update( $new_instance, $old_instance ) { $instance = array(); $instance['title'] = ( !empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; $instance['count'] = ( !empty( $new_instance['count'] ) ) ? strip_tags( $new_instance['count'] ) : '5'; $instance['icon'] = ( !empty( $new_instance['icon'] ) ) ? strip_tags( $new_instance['icon'] ) : ''; $instance['category'] = ( !empty( $new_instance['category'] ) ) ? strip_tags( $new_instance['category'] ) : ''; return $instance; } } // class CategoryPosts add_action( 'widgets_init', function(){ register_widget( 'CategoryPosts' ); });