widget_cssclass = 'avenews-widget-metro-post'; $this->widget_description = __("Metro Widget transforms your posts into a visually appealing mosaic, creating a stylish and organized showcase for an immersive reader experience.", 'avenews'); $this->widget_id = 'Avenews_Metro_Post_Widget'; $this->widget_name = __('Avenews: Metro Post', 'avenews'); $this->settings = array( 'title' => array( 'type' => 'text', 'label' => __('Title', 'avenews'), ), 'layout_style' => array( 'type' => 'select', 'label' => __('Style Layout', 'avenews'), 'options' => array( 'metro-layout-1' => __('Layout 1', 'avenews'), 'metro-layout-2' => __('Layout 2', 'avenews'), 'metro-layout-3' => __('Layout 3', 'avenews'), 'metro-layout-4' => __('Layout 4', 'avenews'), 'metro-layout-5' => __('Layout 5', 'avenews'), ), 'std' => 'metro-layout-1', ), 'category' => array( 'type' => 'dropdown-taxonomies', 'label' => __('Select Category', 'avenews'), 'desc' => __('Leave empty if you don\'t want the posts to be category specific', 'avenews'), 'args' => array( 'taxonomy' => 'category', 'class' => 'widefat', 'hierarchical' => true, 'show_count' => 1, 'show_option_all' => __('— Select —', 'avenews'), ), ), 'show_category' => array( 'type' => 'checkbox', 'label' => __('Show Category', 'avenews'), 'std' => false, ), 'has_category_bg' => array( 'type' => 'checkbox', 'label' => __('Category Background Color', 'avenews'), 'std' => false, ), 'show_author' => array( 'type' => 'checkbox', 'label' => __('Show Author', 'avenews'), 'std' => false, ), 'show_date' => array( 'type' => 'checkbox', 'label' => __('Show Date', 'avenews'), 'std' => true, ), ); parent::__construct(); } /** * Query the posts and return them. * @param array $args * @param array $instance * @return WP_Query */ public function get_posts($args, $instance) { $post_count = ""; switch ($instance['layout_style']) { case "metro-layout-1": $post_count = "5"; break; case "metro-layout-2": $post_count = "4"; break; case "metro-layout-3": $post_count = "5"; break; case "metro-layout-4": $post_count = "5"; break; case "metro-layout-5": $post_count = "3"; break; default: $post_count = ""; } $query_args = array( 'post_status' => 'publish', 'posts_per_page' => $post_count, 'no_found_rows' => 1, 'ignore_sticky_posts' => 1 ); if (!empty($instance['category']) && -1 != $instance['category'] && 0 != $instance['category']) { $query_args['tax_query'][] = array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => $instance['category'], ); } return new WP_Query(apply_filters('Avenews_Metro_Post_Widget_query_args', $query_args)); } /** * Output widget. * * @param array $args * @param array $instance * @see WP_Widget */ public function widget($args, $instance) { ob_start(); echo $args['before_widget']; do_action('avenews_before_metro_widget'); $counter = 1; // Provide a default value for $instance['title'] $title = isset($instance['title']) ? esc_html($instance['title']) : ''; if (($posts = $this->get_posts($args, $instance)) && $posts->have_posts()) { ?>