esc_html__( 'A widget that shows post/page preview', 'biru' ) ) ); } /** * Helper function that holds widget fields * Array is used in update and form functions */ private function widget_fields() { $fields = array( // This widget has no title // Other fields 'post_id' => array ( 'biru_widgets_name' => 'post_id', 'biru_widgets_title' => esc_html__( 'Post ID', 'biru' ), 'biru_widgets_field_type' => 'number' ), 'display_title' => array ( 'biru_widgets_name' => 'display_title', 'biru_widgets_title' => esc_html__( 'Show post title', 'biru' ), 'biru_widgets_field_type' => 'checkbox' ), 'display_thumbnail' => array ( 'biru_widgets_name' => 'display_thumbnail', 'biru_widgets_title' => esc_html__( 'Show featured image', 'biru' ), 'biru_widgets_field_type' => 'checkbox' ), 'display_excerpt' => array ( 'biru_widgets_name' => 'display_excerpt', 'biru_widgets_title' => esc_html__( 'Show excerpt', 'biru' ), 'biru_widgets_field_type' => 'checkbox' ), 'read_more_text' => array ( 'biru_widgets_name' => 'read_more_text', 'biru_widgets_title' => esc_html__( 'Read more link text', 'biru' ), 'biru_widgets_description' => esc_html__( 'Leave empty for no link', 'biru' ), 'biru_widgets_field_type' => 'text' ), ); 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 ); $post_id = $instance['post_id']; $display_title = $instance['display_title']; $display_thumbnail = $instance['display_thumbnail']; $display_excerpt = $instance['display_excerpt']; $read_more_text = $instance['read_more_text']; // No need to do anything if 'post_id' field is empty if( isset( $post_id ) ) { // Check if that ID exists if( $post_object = get_post( $post_id ) ) { echo $before_widget; // Check if title needs to be shown if( $display_title && isset( $post_object->post_title ) ) echo $before_title . $post_object->post_title . $after_title; // Check if thumbnail needs to be shown and if post has thumbnail if( $display_thumbnail && has_post_thumbnail( $post_id ) ) echo '