Support

Account

Home Forums Front-end Issues Repeater field inside custom widget.

Solved

Repeater field inside custom widget.

  • Hi all, I’ve created a custom widget to display some info, it works ok in the back-end but I can’t display the data on the front-end. It worked fine when I was using simple fields but I realized that repeater fields are better for this.

    This is what I have so far…

    add_action( 'widgets_init', 'services_widget' );
    function services_widget() {
        register_widget( 'services_widget' );
    }
     
    class services_widget extends WP_Widget {
     
        public function __construct() {
            $widget_ops = array(
                'classname' => 'services_widget',
    	    'description' => 'Add a service description.'
            );
    	$control_ops = array( 'width' => 400, 'height' => 350 );
            parent::__construct( 'services_widget', 'Services', $widget_ops, $control_ops );
        }
    	
    	public function widget( $args, $instance ) {
    		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
    		echo $args['before_widget'];
    		if ( ! empty( $title ) ) {
    		echo $args['before_title'] . $title .  $args['after_title'];
    		} 
    		if( have_rows('service', 'widget_' . $widget_id) ):
    		echo '<ul>';
    		while ( have_rows('service', 'widget_' . $widget_id) ) : the_row();
    		echo '<li class="service one-half">';
    		$title = get_sub_field( 'title', 'widget_' . $widget_id );
    		$body = get_sub_field( 'body', 'widget_' . $widget_id );
    		$button = get_sub_field( 'button', 'widget_' . $widget_id );
    		$button_link = get_sub_field( 'button_link',  'widget_' . $widget_id );
    		if( $title ) {
    		echo '<h4>' . $title . '</h4>';
    		} 
    		if( $body ) {
    		echo '<p>' . $body . '</p>';
    		}
    		if( $body ) {
    		echo '<a class="more" href="' . $button_link . '">' .$button . '</a>';
    		}
    		echo '</li><div class="clearfix"></div></ul>';
    		endwhile;
    		endif;
    		echo $args['after_widget'];
    	}
    	
    	public function form( $instance ) {
    		$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
    		$filter = isset( $instance['filter'] ) ? $instance['filter'] : 0;
    		$title = sanitize_text_field( $instance['title'] );
    		?>
    		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
    		<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
    		<p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox"<?php checked( $filter ); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
    		<?php
    	}
    
    	public function update( $new_instance, $old_instance ) {
    		$instance = $old_instance;
    		$instance['title'] = sanitize_text_field( $new_instance['title'] );
    		if ( current_user_can( 'unfiltered_html' ) ) {
    		$instance['text'] = $new_instance['text'];
    		} else {
    		$instance['text'] = wp_kses_post( $new_instance['text'] );
    		}
    		$instance['filter'] = ! empty( $new_instance['filter'] );
    		return $instance;
    	}
    }

    I appreciate any help with this, I’m sure there’s something wrong with the code, it’s my first time creating a widget.

  • Solved!

    I replaced $widget_id with $args['widget_id']

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Repeater field inside custom widget.’ is closed to new replies.