Support

Account

Home Forums Front-end Issues Repeater display in custom widget

Solved

Repeater display in custom widget

  • I have set up 2 fields in a custom widget that I created:

    1. A select field with 3 options
    2. A repeater with 2 number sub fields (both number inputs, a ‘max’ and a ‘min’)

    In my “widget” function, I can display the select field value successfully, but when try to loop through the repeater field, nothing happens. Here’s my code:

    
    <?php
    
    // register WooCommerce dimensions filter widget
    add_action( 'widgets_init', function(){
    	register_widget( 'G2_Dimensions_Filter_Widget' );
    });
    
    class G2_Dimensions_Filter_Widget extends WP_Widget {
    	// class constructor
    	public function __construct() {
    		$widget_ops = array( 
    			'classname' => 'dimensions_filter_widget',
    			'description' => 'Filter products by shipping dimensions',
    		);
    		parent::__construct( 'dimensions_filter_widget', 'WooCommerce Dimensions Filter', $widget_ops );
    	}
    	
    	// output the widget content on the front-end
    	public function widget( $args, $instance ) {
    		// outputs the content of the widget
    		if ( ! isset( $args['widget_id'] ) ) {
    			$args['widget_id'] = $this->id;
    		}
    
    		$w_id = 'widget_' . $args['widget_id'];
    
    		$dimension_type = get_field('dimension_filter_type', $w_id);
    		$title = 'By ' . $dimension_type;
    
    		// Display accordion title/trigger and opening accordion content wrapper
    		echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title'] . '<ul class="dimesions-filters">';
    
    		/* DIMENSIONS OPTIONS LOOP */
    		while (have_rows('dimensions_filter_options', $w_id)) {
    			the_row();
    
    			echo '<li class="wc-layered-nav-term">';
    			echo '<a href="">';
    			the_sub_field('min', $w_id);
    			echo ' &dash; ';
    			the_sub_field('max', $w_id);
    			echo '</a>';
    			echo '</li>';
    		}
    		/* DIMENSIONS OPTIONS LOOP */
    
    		// Display closing accordion content wrapper
    		echo '</ul>' . $args['after_widget'];
    	}
    
    	// output the option form field in admin Widgets screen
    	public function form( $instance ) {
    		/*************************************************/
    		/*** Widget fields are implemented via ACF PRO ***/
    		/*************************************************/
    	}
    
    	// save options
    	public function update( $new_instance, $old_instance ) {}
    }
    

    What REALLY threw me for a loop is when I tried simply grabbing the “Dimensions Filter Options” (repeater) via get_field and it simply returned the count of the rows in the field (instead of an array of the values for that individual widget instance).

    My test code looked like this:

    
    $dimensions_opts = get_field('dimensions_filter_options', $w_id);
    echo '<pre>';
    var_dump($dimensions_opts);
    echo '</pre>';
    

    The result looked like this:

    string(1) "4"

    On the front end, I noticed that my loop is not being acknowledged at all. Can anyone spot where I might be going wrong?

  • I discovered the problem. It wasn’t related to ACF, at all.

    I had an error in a pre_get_posts declaration, which was affecting the loop. I was using is_main_query() instead of $query->is_main_query().

    I removed the problematic code, and it works like a charm.

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

The topic ‘Repeater display in custom widget’ is closed to new replies.