'anderson_category_posts_boxed', 'description' => __('Displays your posts from a selected category in a boxed layout. Please use this widget ONLY in the Magazine Homepage widget area.', 'anderson-lite') ); parent::__construct('anderson_category_posts_boxed', sprintf( __('Category Posts: Boxed (%s)', 'anderson-lite'), 'Anderson' ), $widget_ops); // Delete Widget Cache on certain actions add_action( 'save_post', array( $this, 'delete_widget_cache' ) ); add_action( 'deleted_post', array( $this, 'delete_widget_cache' ) ); add_action( 'switch_theme', array( $this, 'delete_widget_cache' ) ); } public function delete_widget_cache() { wp_cache_delete('widget_anderson_category_posts_boxed', 'widget'); } private function default_settings() { $defaults = array( 'title' => '', 'category' => 0, 'category_link' => false ); return $defaults; } // Display Widget function widget($args, $instance) { $cache = array(); // Get Widget Object Cache if ( ! $this->is_preview() ) { $cache = wp_cache_get( 'widget_anderson_category_posts_boxed', 'widget' ); } if ( ! is_array( $cache ) ) { $cache = array(); } // Display Widget from Cache if exists if ( isset( $cache[ $this->id ] ) ) { echo $cache[ $this->id ]; return; } // Start Output Buffering ob_start(); // Get Sidebar Arguments extract($args); // Get Widget Settings $defaults = $this->default_settings(); extract( wp_parse_args( $instance, $defaults ) ); // Output echo $before_widget; ?>
is_preview() ) { $cache[ $this->id ] = ob_get_flush(); wp_cache_set( 'widget_anderson_category_posts_boxed', $cache, 'widget' ); } else { ob_end_flush(); } } // Render Widget Content function render($instance) { // Get Widget Settings $defaults = $this->default_settings(); extract( wp_parse_args( $instance, $defaults ) ); // Get latest posts from database $query_arguments = array( 'posts_per_page' => 6, 'ignore_sticky_posts' => true, 'cat' => (int)$category ); $posts_query = new WP_Query($query_arguments); $i = 0; // Check if there are posts if( $posts_query->have_posts() ) : // Limit the number of words for the excerpt add_filter('excerpt_length', 'anderson_category_posts_widgets_excerpt_length'); // Display Posts while( $posts_query->have_posts() ) : $posts_query->the_post(); if(isset($i) and $i == 0) : ?>
__('All Categories', 'anderson-lite'),
'show_count' => true,
'hide_empty' => false,
'selected' => $category,
'name' => $this->get_field_name('category'),
'id' => $this->get_field_id('category')
);
wp_dropdown_categories( $args );
?>