1

I have a problem when retrieving acf fields added for a widget. I will explain the situation.

I am using MaxMegamenu plugin for implementing megamenu, ACF pro for custom fields and WPML for site translatiom. Primary language is Deutsch and English is secondary.

For megamenu content I have created a custom widget "Submegamenu". And for adding megamenu data, also created necessary acf field for it.

Then created the Menu, added the widget to megamenu and added content to acf fields in widget in German language. The same process is repeated for English language in the site.

Widget added to maxmegamenu, Acf fields inside the widget, language setting for widget

Inside the widget code, calling the acf fields get_field() with widget id retrieves all the data correctly in German. But no data is showing in English.

All the fields are made translatable.

I tried to figure it out and I found something that, when I add each Submegamenu widget to Menu item, wordpress will create a completely new instance of widget with different IDs on the options table. I can access the ID but not the acf fields on frontend.

I don't know this is the correct way of implementing it.

The great helping hands will be much appreciated...! Thank you..

code for reference:

class Submegamenu_widget extends WP_Widget {
  
    function __construct() {
        parent::__construct(
            'submegamenu_widget', // Base ID
            __('Submegamenu Widget', 'textdomain'), // Widget name
            array( 'description' => __( 'Widget to implement megamenu', 'textdomain' ), )// Widget description
        );
    }
    
    // Creating widget front-end
    
    public function widget( $args, $instance ) {
        
        // before and after widget arguments are defined by themes
        echo $args['before_widget'];
        /**
         * calls the acf field data
         * Here is the problem
         * In German this works fine.
         * In English, this will return null
         */
         $teaser_block = get_field('teaser_block', 'widget_'.$args['widget_id'] );


        // html goes here



        echo $args['after_widget'];
    }
            
    // Widget Backend 
    public function form( $instance ) {
        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        }
        else {
            $title = __( 'New title', 'hamborner' );
        }
    }
        
    // Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        return $instance;
    }
 

} 

0 Answers0