Name ), // Name array( 'classname' => 'gridbox_magazine_posts_grid', 'description' => esc_html__( 'Displays your posts from a selected category in a grid layout. Please use this widget ONLY in the Magazine Homepage widget area.', 'gridbox' ) ) // Args ); // 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' ) ); } /** * Set default settings of the widget */ private function default_settings() { $defaults = array( 'title' => '', 'category' => 0, 'layout' => 'three-columns-grid', 'number' => 3, 'excerpt_length' => 0, 'meta_date' => true, 'meta_author' => true, 'meta_category' => true, ); return $defaults; } /** * Main Function to display the widget * * @uses this->render() * * @param array $args / Parameters from widget area created with register_sidebar() * @param array $instance / Settings for this widget instance */ function widget( $args, $instance ) { $cache = array(); // Get Widget Object Cache if ( ! $this->is_preview() ) { $cache = wp_cache_get( 'widget_gridbox_magazine_posts_grid', '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 Widget Settings $settings = wp_parse_args( $instance, $this->default_settings() ); // Set Excerpt Length $this->excerpt_length = (int)$settings['excerpt_length']; // Output echo $args['before_widget']; ?>
is_preview() ) { $cache[ $this->id ] = ob_get_flush(); wp_cache_set( 'widget_gridbox_magazine_posts_grid', $cache, 'widget' ); } else { ob_end_flush(); } } // widget() /** * Renders the Widget Content * * Switches between two or three column layout style based on widget settings * * @uses this->magazine_posts_two_column_grid() or this->magazine_posts_three_column_grid() * @used-by this->widget() * * @param array $settings / Settings for this widget instance */ function render( $settings ) { // Get latest posts from database $query_arguments = array( 'posts_per_page' => (int)$settings['number'], 'ignore_sticky_posts' => true, 'cat' => (int)$settings['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', array( $this, 'excerpt_length' ) ); ?>
esc_html__( 'All Categories', 'gridbox' ),
'show_count' => true,
'hide_empty' => false,
'selected' => $settings['category'],
'name' => $this->get_field_name('category'),
'id' => $this->get_field_id('category')
);
wp_dropdown_categories( $args );
?>