'admiral-magazine-sidebar-widget', 'description' => esc_html__( 'Displays your posts from a selected category. You can use this widget in the Main Sidebar widget area.', 'admiral' ), 'customize_selective_refresh' => true, ) // Args. ); } /** * Set default settings of the widget */ private function default_settings() { $defaults = array( 'title' => esc_html__( 'Magazine (Sidebar)', 'admiral' ), 'category' => 0, 'number' => 4, 'excerpt' => false, ); 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 ) { // Start Output Buffering. ob_start(); // Get Widget Settings. $settings = wp_parse_args( $instance, $this->default_settings() ); // Output. echo $args['before_widget']; ?>
magazine_posts_two_column_sidebar() or this->magazine_posts_three_column_sidebar() * @used-by this->widget() * * @param array $settings / Settings for this widget instance. */ function render( $settings ) { // Get cached post ids. $post_ids = admiral_get_magazine_post_ids( $this->id, $settings['category'], $settings['number'] ); // Fetch posts from database. $query_arguments = array( 'post__in' => $post_ids, 'posts_per_page' => absint( $settings['number'] ), 'ignore_sticky_posts' => true, 'no_found_rows' => true, ); $posts_query = new WP_Query( $query_arguments ); // Check if there are posts. if ( $posts_query->have_posts() ) : // Limit the number of words for the excerpt. add_filter( 'excerpt_length', 'admiral_magazine_posts_excerpt_length' ); // Pass Excerpt Setting to template file. set_query_var( 'admiral_post_excerpt', (bool) $settings['excerpt'] ); // Display Posts. while ( $posts_query->have_posts() ) : $posts_query->the_post(); get_template_part( 'template-parts/widgets/magazine-large-post', 'sidebar' ); endwhile; // Remove excerpt filter. remove_filter( 'excerpt_length', 'admiral_magazine_posts_excerpt_length' ); endif; // Reset Postdata. wp_reset_postdata(); } /** * Displays Widget Title * * @param array $args / Parameters from widget area created with register_sidebar(). * @param array $settings / Settings for this widget instance. */ function widget_title( $args, $settings ) { // Add Widget Title Filter. $widget_title = apply_filters( 'widget_title', $settings['title'], $settings, $this->id_base ); if ( ! empty( $widget_title ) ) : // Link Widget Title to category archive when possible. $widget_title = admiral_magazine_widget_title( $widget_title, $settings['category'] ); // Display Widget Title. echo $args['before_title'] . $widget_title . $args['after_title']; endif; } /** * Update Widget Settings * * @param array $new_instance / New Settings for this widget instance. * @param array $old_instance / Old Settings for this widget instance. * @return array $instance */ function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = sanitize_text_field( $new_instance['title'] ); $instance['category'] = (int) $new_instance['category']; $instance['number'] = (int) $new_instance['number']; $instance['excerpt'] = ! empty( $new_instance['excerpt'] ); admiral_flush_magazine_post_ids(); return $instance; } /** * Displays Widget Settings Form in the Backend * * @param array $instance / Settings for this widget instance. */ function form( $instance ) { // Get Widget Settings. $settings = wp_parse_args( $instance, $this->default_settings() ); ?>
esc_html__( 'All Categories', 'admiral' ),
'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 );
?>