'news_portal_featured_slider', 'description' => __( 'Displays posts from selected categories in the slider with the featured section.', 'news-portal' ) ); parent::__construct( 'news_portal_featured_slider', __( 'NP: Featured Slider', 'news-portal' ), $widget_ops ); } /** * Helper function that holds widget fields * Array is used in update and form functions */ private function widget_fields() { $fields = array( 'slider_cat_ids' => array( 'news_portal_widgets_name' => 'slider_cat_ids', 'news_portal_widgets_title' => __( 'Slider Categories', 'news-portal' ), 'news_portal_widgets_field_type' => 'multicheckboxes', 'news_portal_widgets_field_options' => news_portal_categories_lists() ), 'featured_cat_ids' => array( 'news_portal_widgets_name' => 'featured_cat_ids', 'news_portal_widgets_title' => __( 'Featured Post Categories', 'news-portal' ), 'news_portal_widgets_field_type' => 'multicheckboxes', 'news_portal_widgets_field_options' => news_portal_categories_lists() ) ); 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 ); if ( empty( $instance ) ) { return ; } $news_portal_slider_cat_ids = empty( $instance['slider_cat_ids'] ) ? '' : $instance['slider_cat_ids']; $news_portal_featured_cat_ids = empty( $instance['featured_cat_ids'] ) ? '' : $instance['featured_cat_ids']; echo $before_widget; ?>
$cat_value ){ $checked_cats[] = $cat_key; } $get_cats_ids = implode( ",", $checked_cats ); $news_portal_post_count = apply_filters( 'news_portal_slider_posts_count', 4 ); $news_portal_slider_args = array( 'post_type' => 'post', 'cat' => $get_cats_ids, 'posts_per_page' => absint( $news_portal_post_count ) ); $news_portal_slider_query = new WP_Query( $news_portal_slider_args ); if ( $news_portal_slider_query->have_posts() ) { echo '
widget_fields(); // Loop through fields foreach ( $widget_fields as $widget_field ) { extract( $widget_field ); // Use helper function to get updated field values $instance[$news_portal_widgets_name] = news_portal_widgets_updated_field_value( $widget_field, $new_instance[$news_portal_widgets_name] ); } return $instance; } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. * * @uses news_portal_widgets_show_widget_field() defined in np-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 ); $news_portal_widgets_field_value = !empty( $instance[$news_portal_widgets_name] ) ? $instance[$news_portal_widgets_name] : ''; news_portal_widgets_show_widget_field( $this, $widget_field, $news_portal_widgets_field_value ); } } }