the_widget() and widget’s ID

Let’s assume I have a widget that displays only its name:

    <p>
         <?php echo $args['widget_id'] ?>
    </p>

So when I drag & drop my widget to any sidebar it shows:

<p>
     myWidget-number
</p>

The problem is I want to call this widget with a shortcode:

    (...)
            ob_start();    
            the_widget(MyWidget);
            return ob_get_clean();
   }

    add_shortcode('myWidget_short', 'myWidget_shortcode');

And when i do [myWidget_short] it shows only

<p>
</p>

Any ideas how to call widget’s ID with a shortcode?

2 Answers
2

I believe @One Trick Pony was right.

Shortcode widgets have no ID, so I’ve found a way around.

Firstly I used PHP rand function:

$var = rand();

And then added the “var” to the ID, so it doesn’t collide with other shortcodes calling the same widget (each one has different random number at the end of the ID):

<div id="myWidget-<?php echo $var?>;"></div>

Leave a Comment