• Resolved mlyczko

    (@mlyczko)


    Hi

    My problem is that i’m using
    $this->id
    in my widget’s code to show widget’s ID. It’s working correctly if i display few widgets in some sidebar but there is a problem with Widget Shortcode – every widget displays ID: “-1” instead of for example “my_widget-5”.
    Is there maybe a workaround?

    Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author shazdeh

    (@shazdeh)

    Hi,

    I’m missing the context for this issue ?? Where are you adding the $this->id code? Does $this->id fail if you have the Widget Shortcode active (and it works otherwise)? Any guide on how I could replicate this issue?

    Thread Starter mlyczko

    (@mlyczko)

    Hi

    Please take a look here: https://ml.dev.pax1.eu/testy/wordpress/
    There are 3 widgets in content and 3 widgets in sidebar.
    There are wrong IDs in content: https://www.screencast.com/t/FBN5zGQNt8f
    OK in sidebar: https://www.screencast.com/t/ofPp9Tnk

    Thanks

    Plugin Author shazdeh

    (@shazdeh)

    I see. I checked and the -1 ID is set specifically by the the_widget function which is what the plugin uses in the plugin to display the widget outside a widget area.
    Might I ask why you want this? Note that the widget wrapper has the proper ID, and if you wish to add the widget ID as a classname you could do:

    
    <?php
    
    function custom_widget_shortcode_classes( $atts ) {
    	$atts['container_class'] .= ' widget-%1$s';
    	return $atts;
    }
    add_filter( 'shortcode_atts_widget', 'custom_widget_shortcode_classes' );
    

    The widget-{widgetID} class is now added to all widgets displayed by the plugin.

    Thread Starter mlyczko

    (@mlyczko)

    Hi

    Thank you.
    I have this code inside plugin:

    		<script>
    			jQuery(document).ready(
    				function($)
    				{
    				    $('#<?php echo $this->id; ?> .pe-recent-posts-outer').carousel({
    				    	interval: <?php echo $interval; ?>,
    						pause: "<?php echo $slider_pause; ?>"
    				    })
    				}
    			);
    		</script>

    I need a some way to get widget ID inside the plugin’s code.

    Thanks.

    Plugin Author shazdeh

    (@shazdeh)

    Hi!

    Inside the widget( $args, $instance ) method, you can access $args['widget_id'] and that properly outputs the widget ID. This is not overrided with a fake ID and so it should work as you expect.

    Another alternative (which I personally prefer) would be to ditch the inline <script> tag and instead do it like so:

    
    <script>
    jQuery( function($) {
    	$( '.pe-recent-posts-outer' ).each( function(){
    		var $this = $( this );
    		$this.carousel({
    			interval: $this.data( 'interval' ),
    			pause: $this.data( 'pause' )
    		});
    	} );
    } );
    </script>
    

    And in your widget output, you’d use data-interval="" HTML attribute; this way the same JavaScript code is used for all the widgets and their parameters are dynamic as well.

    Hope this helps!

    Thread Starter mlyczko

    (@mlyczko)

    Great support.

    Thank you very much ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Using few the same widgets – wrong ID’ is closed to new replies.