Home › Forums › Front-end Issues › 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 ' ‐ ';
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.
The topic ‘Repeater display in custom widget’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.