How to add widget text to index.php file using foreach loop or any other way

enter image description here

Any body who help me to make widget for add text to div using foreach loop or any other loop or way to enter data in below html example
Kind Regard.

admin Panel for add data to widget now i want to display data to my index.php file

    /**
     * Register widget with WordPress.
     */
    function __construct() {
        parent::__construct(
            'Slider', // Base ID
            esc_html__( 'Slide Section', 'text_domain' ), // Name
            array( 'description' => esc_html__( 'Fill input feild for show on your website', 'text_domain' ), ) // Args
        );
    }

    /**
     * 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 ) {
        echo $args['before_widget'];
        if ( ! empty( $instance['text_1'] ) ) {
            echo $args['before_text_1'] . apply_filters( 'widget_text_1', $instance['text_1'] ) . $args['after_text_1'];
        }
        if ( ! empty( $instance['text_2'] ) ) {
            echo $args['before_text_2'] . apply_filters( 'widget_text_2', $instance['text_2'] ) . $args['after_text_2'];
        }
        if ( ! empty( $instance['text_3'] ) ) {
            echo $args['before_text_3'] . apply_filters( 'widget_text_3', $instance['text_3'] ) . $args['after_text_3'];
        }
        if ( ! empty( $instance['text_4'] ) ) {
            echo $args['before_text_4'] . apply_filters( 'widget_text_4', $instance['text_4'] ) . $args['after_text_4'];
        }
        if ( ! empty( $instance['text_5'] ) ) {
            echo $args['before_text_5'] . apply_filters( 'widget_text_5', $instance['text_5'] ) . $args['after_text_5'];
        }
        echo esc_html__( 'Hello, World!', 'text_domain' );
        echo $args['after_widget'];
    }

    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     */
    public function form( $instance ) {
        $text_1 = ! empty( $instance['text_1'] ) ? $instance['text_1'] : esc_html__( '', 'text_domain' );
        $text_2 = ! empty( $instance['text_2'] ) ? $instance['text_2'] : esc_html__( '', 'text_domain' );
        $text_3 = ! empty( $instance['text_3'] ) ? $instance['text_3'] : esc_html__( '', 'text_domain' );
        $text_4 = ! empty( $instance['text_4'] ) ? $instance['text_4'] : esc_html__( '', 'text_domain' );
        $text_5 = ! empty( $instance['text_5'] ) ? $instance['text_5'] : esc_html__( '', 'text_domain' );
        ?>

        <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'text_1' ) ); ?>"><?php esc_attr_e( 'Text 1:', 'text_domain1' ); ?></label> 
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'text_1' ) ); ?>" name="<?php echo esc_attr($this->get_field_name('text_1')); ?>" type="text" value="<?php echo esc_attr( $text_1 ); ?>" placeholder="Type Slides Tag line 1" >
        </p>

        <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'text_2' ) ); ?>"><?php esc_attr_e( 'Text 2:', 'text_domain2' ); ?></label> 
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'text_2' ) ); ?>" name="<?php echo esc_attr($this->get_field_name('text_2')); ?>" type="text" value="<?php echo esc_attr( $text_2 ); ?>" placeholder="Type Slides Tag line 2 ">
        </p>

        <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'text_3' ) ); ?>"><?php esc_attr_e( 'Text 3:', 'text_domain3' ); ?></label> 
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'text_3' ) ); ?>" name="<?php echo esc_attr($this->get_field_name('text_3')); ?>" type="text" value="<?php echo esc_attr( $text_3 ); ?>" placeholder="Type Slides Tag line 3 ">
        </p>

        <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'text_4' ) ); ?>"><?php esc_attr_e( 'Text 4:', 'text_domain4' ); ?></label> 
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'text_4' ) ); ?>" name="<?php echo esc_attr($this->get_field_name('text_4')); ?>" type="text" value="<?php echo esc_attr( $text_4 ); ?>" placeholder="Type Slides Tag line 4 ">
        </p>

        <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'text_5' ) ); ?>"><?php esc_attr_e( 'Url:', 'text_domain5' ); ?></label> 
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'text_5' ) ); ?>" name="<?php echo esc_attr($this->get_field_name('text_5')); ?>" type="text" value="<?php echo esc_attr( $text_5 ); ?>"placeholder="Paste link of page for slides detail ">
        </p>

        <?php 
    }

    /**
     * Sanitize widget form values as they are saved.
     *
     * @see WP_Widget::update()
     *
     * @param array $new_instance Values just sent to be saved.
     * @param array $old_instance Previously saved values from database.
     *
     * @return array Updated safe values to be saved.
     */
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['text_1'] = ( ! empty( $new_instance['text_1'] ) ) ? strip_tags( $new_instance['text_1'] ) : '';
        $instance['text_2'] = ( ! empty( $new_instance['text_2'] ) ) ? strip_tags( $new_instance['text_2'] ) : '';
        $instance['text_3'] = ( ! empty( $new_instance['text_3'] ) ) ? strip_tags( $new_instance['text_3'] ) : '';
        $instance['text_4'] = ( ! empty( $new_instance['text_4'] ) ) ? strip_tags( $new_instance['text_4'] ) : '';
        $instance['text_5'] = ( ! empty( $new_instance['text_5'] ) ) ? strip_tags( $new_instance['text_5'] ) : '';
        return $instance;}}
// register Foo_Widget widget
function register_foo_widget() {
   register_widget( 'Slider' );
    register_sidebar( array(
        'name' => __( 'Main Slider', 'theme-slug' ),
        'id' => 'sidebar-1',
        'description' => __( 'Widgets in this area will be shown on Main homepage', 'theme-slug' ),
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget'  => '</li>',
        'before_text_1'  => '<h2 class="widgettext_1">',
        'after_text_1'   => '</h2>',
    ) );
}
add_action( 'widgets_init', 'register_foo_widget' );
//add_action( 'widgets_init', 'theme_slug_widgets_init' );

Read more here: How to add widget text to index.php file using foreach loop or any other way

Leave a Reply

Your email address will not be published. Required fields are marked *